解题思路
先统计连续的0和1分别有多少个,如:111100011000,得到4323;在4323中的任意相邻两个数字,取小的一个加起来,就是3+2+2 = 7.
代码
class Solution: def countBinarySubstrings(self, s: str) -> int: list = [] count = 1 sum_number = 0 for i in range(1,len(s)): if s[i] == s[i-1]: count += 1 else: list.append(count) count = 1 list.append(count) for i in range(len(list)-1): sum_number += min(list[i],list[i+1]) return sum_number