Easy
0
和1
两种字符,我们可以统计连续的字符的数量,这样,前后连续数量相同的字串,就是相邻两个计数中,较少的值。以上,尝试写一下代码,AC!
python3
class Solution:
def countBinarySubstrings(self, s: str) -> int:
cnts = []
i = 0
j = 1
while i < len(s) and j < len(s):
if s[j] != s[i]:
cnts.append(j-i)
i = j
j = i + 1
else:
j += 1
cnts.append(j-i)
res = 0
for k in range(1,len(cnts)):
res += min(cnts[k], cnts[k-1])
return res