1869. 哪种连续子字符串更长
1869. 哪种连续子字符串更长
题目
Given a binary string s, return true _if the longest contiguous segment of _1'_s is strictly longer than the longest contiguous segment of _0's ins, or return false otherwise.
- For example, in
s = "_11_ 01 _000_ 10"the longest continuous segment of1s has length2, and the longest continuous segment of0s has length3.
Note that if there are no 0's, then the longest continuous segment of 0's is considered to have a length 0. The same applies if there is no 1's.
Example 1:
Input: s = "1101"
Output: true
Explanation:
The longest contiguous segment of 1s has length 2: "11 01"
The longest contiguous segment of 0s has length 1: "11 0 1"
The segment of 1s is longer, so return true.
Example 2:
Input: s = "111000"
Output: false
Explanation:
The longest contiguous segment of 1s has length 3: "111 000"
The longest contiguous segment of 0s has length 3: "111 000 "
The segment of 1s is not longer, so return false.
Example 3:
Input: s = "110100010"
Output: false
Explanation:
The longest contiguous segment of 1s has length 2: "11 0100010"
The longest contiguous segment of 0s has length 3: "1101 000 10"
The segment of 1s is not longer, so return false.
Constraints:
1 <= s.length <= 100s[i]is either'0'or'1'.
题目大意
给你一个二进制字符串 s 。如果字符串中由 1 组成的 最长 连续子字符串 严格长于 由 0 组成的 最长 连续子字符串,返回 true ;否则,返回 false。
- 例如,
s = "110100010"中,由1组成的最长连续子字符串的长度是2,由0组成的最长连续子字符串的长度是3。
注意,如果字符串中不存在 0 ,此时认为由 0 组成的最长连续子字符串的长度是 0 。字符串中不存在 1 的情况也适用此规则。
示例 1:
输入: s = "1101"
输出: true
解释:
由 1 组成的最长连续子字符串的长度是 2:"11 01"
由 0 组成的最长连续子字符串的长度是 1:"110 1"
由 1 组成的子字符串更长,故返回 true 。
示例 2:
输入: s = "111000"
输出: false
解释:
由 1 组成的最长连续子字符串的长度是 3:"111 000"
由 0 组成的最长连续子字符串的长度是 3:"111000 "
由 1 组成的子字符串不比由 0 组成的子字符串长,故返回 false 。
示例 3:
输入: s = "110100010"
输出: false
解释:
由 1 组成的最长连续子字符串的长度是 2:"11 0100010"
由 0 组成的最长连续子字符串的长度是 3:"1101000 10"
由 1 组成的子字符串不比由 0 组成的子字符串长,故返回 false 。
提示:
1 <= s.length <= 100s[i]不是'0'就是'1'
解题思路
初始化变量:
curOnes和curZeros用于记录当前连续出现的 1 和 0 的个数。maxOnes和maxZeros用于记录字符串中出现的连续 1 和连续 0 的最大个数。
遍历字符串:
- 遍历字符串中的每个字符:
- 如果字符是
1,则:- 增加
curOnes,并更新maxOnes为curOnes和当前maxOnes的较大值。 - 重置
curZeros为 0,表示当前连续的 0 断开了。
- 增加
- 如果字符是
0,则:- 增加
curZeros,并更新maxZeros为curZeros和当前maxZeros的较大值。 - 重置
curOnes为 0,表示当前连续的 1 断开了。
- 增加
- 如果字符是
- 遍历字符串中的每个字符:
最终判断:
- 遍历结束后,直接比较
maxOnes和maxZeros的大小,返回maxOnes > maxZeros。
- 遍历结束后,直接比较
复杂度分析
- 时间复杂度:
O(n),其中n是字符串s的长度,因为只需要遍历一次字符串。 - 空间复杂度:
O(1),只用了常数级别的额外空间。
代码
/**
* @param {string} s
* @return {boolean}
*/
var checkZeroOnes = function (s) {
let curOnes = 0,
curZeros = 0;
let maxOnes = 0,
maxZeros = 0;
for (let char of s) {
if (char == '1') {
curOnes++;
maxOnes = Math.max(maxOnes, curOnes);
curZeros = 0; // 重置当前连续 0 的计数
} else {
curZeros++;
maxZeros = Math.max(maxZeros, curZeros);
curOnes = 0; // 重置当前连续 1 的计数
}
}
return maxOnes > maxZeros; // 比较连续 1 和连续 0 的最大值
};
相关题目
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
|---|---|---|---|---|---|
| 485 | 最大连续 1 的个数 | [✓] | 数组 | 🟢 | 🀄️ 🔗 |
| 1784 | 检查二进制字符串字段 | [✓] | 字符串 | 🟢 | 🀄️ 🔗 |
| 2031 | 1 比 0 多的子数组个数 🔒 | 树状数组 线段树 数组 4+ | 🟠 | 🀄️ 🔗 |
