The title

I give you a binary string s. Returns true if the longest contiguous substring of a string is strictly longer than the longest contiguous substring of a string of 0; Otherwise, return false.

For example, in s = “110100010”, the length of the longest continuous substring composed of 1 is 2, and the length of the longest continuous substring composed of 0 is 3. Note that if there is no 0 in the string, the length of the longest continuous substring of 0 is considered to be 0. This rule also applies if there is no 1 in the string.

Example 1: Input: s = "1101" Output: true Description: The length of the longest continuous substring consisting of 1s is 2: "1101" The length of the longest continuous substring consisting of 0s is 1: "1101" The length of a substring consisting of 1s is longer, so return true. Example 2: Input: s = "111000" Output: false Description: The length of the longest continuous substring consisting of 1s is 3: the length of the longest continuous substring consisting of 0s is 3: The substring of "111000" is not longer than the substring of 0, so return false. Example 3: Input: s = "110100010" Output: false Description: The length of the longest continuous substring consisting of 1s is 2: "110100010" The length of the longest continuous substring consisting of 0s is 3: "110100010" the substring of 1 is not longer than the substring of 0, so return false.Copy the code

Tip:

1 <= s.length <= 100 s[I] either ‘0’ or ‘1’

Their thinking

class Solution: def checkZeroOnes(self, s: str) -> bool: zeroNum, oneNum=0, 0 zeroMaxNum, oneMaxNum = 0, 0 for i in s: if i == "1": oneNum += 1 if zeroNum ! = 0: zeroMaxNum = max(zeroMaxNum, zeroNum) zeroNum = 0 elif i == "0": zeroNum += 1 if oneNum ! = 0: OneMaxNum = Max (oneMaxNum, oneNum) oneNum = 0 # print (oneNum zeroNum, oneMaxNum, zeroMaxNum) # traversal after the last step to judge if zeroNum! = 0: zeroMaxNum = max(zeroMaxNum, zeroNum) zeroNum = 0 if oneNum ! = 0: oneMaxNum = max(oneMaxNum, oneNum) oneNum = 0 if oneMaxNum > zeroMaxNum: return True else: return False if __name__ == '__main__': # s = "1101" # s = "111000" s = "110100010" ret = Solution().checkZeroOnes(s) print(ret)Copy the code