This is an easy level leetcode problem. Please refer to this link for the detail of the problem description.
Basically, the idea is to find the longest consecutive 1’s and 0’s and return True if 1’s > 0’s.
There are a couple of ways to achieve the problem.
The first one is a pythonic way. Since the string is a binary string that only has 1 and 0, you can split the string based on them. After the split, it’s really to find the longest string in the array. But this solution is only applicable to python.
class Solution: def checkZeroOnes(self, s: str) -> bool: return max((len(digit) for digit in s.split('0'))) > max((len(digit) for digit in s.split('1')))
Another solution is to explicitly iterate the string and count num 1’s and 0’s which are applicable to all other languages. You can do it in one pass as you see the solution below.
class Solution: def checkZeroOnes(self, s: str) -> bool: ones_start_idx = 0 num_ones = 0 zeros_start_idx = 0 num_zeros = 0 for idx, digit in enumerate(s): if digit == '0': ones_start_idx = idx + 1 num_zeros = max(num_zeros, idx - zeros_start_idx + 1) else: zeros_start_idx = idx + 1 num_ones = max(num_ones, idx - ones_start_idx + 1) return num_ones > num_zeros