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 in s
. Return false
otherwise.
- For example, in
s = "110100010"
the longest contiguous segment of1
s has length2
, and the longest contiguous segment of0
s has length3
.
Note that if there are no 0
s, then the longest contiguous segment of 0
s is considered to have length 0
. The same applies if there are no 1
s.
Example 1:
Input: s = "1101" Output: true Explanation: The longest contiguous segment of 1s has length 2: "1101" The longest contiguous segment of 0s has length 1: "1101" 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: "111000" The longest contiguous segment of 0s has length 3: "111000" 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: "110100010" The longest contiguous segment of 0s has length 3: "110100010" The segment of 1s is not longer, so return false.
Constraints:
1 <= s.length <= 100
s[i]
is either'0'
or'1'
.
Solution: Brute Force
Write a function count to count longest contiguous segment of m, return count(‘1’) > count(‘0’)
Time complexity: O(n)
Space complexity: O(1)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Author: Huahua class Solution { public: bool checkZeroOnes(string s) { auto count = [&](char m) { int ans = 0; int l = 0; for (char c : s) if (c != m) l = 0; else ans = max(ans, ++l); return ans; }; return count('1') > count('0'); } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment