https://leetcode-cn.com/problems/count-number-of-homogenous-substrings/https://leetcode-cn.com/problems/count-number-of-homogenous-substrings/https://github.com/w5678/leetcode-2021https://github.com/w5678/leetcode-2021
class Solution:
def countHomogenous(self, s: str) -> int:
if not s:
return 0
i,cnt=0,0
last=s[0]
inc=0
while i <len(s):
if last == s[i]:
inc+=1
else:
inc=1
last = s[i]
i+=1
cnt+=inc
return cnt%((10**9)+7)
思路: 1,遍历所有的字符,保存上一个字符为last,与当前字符比较,如果相同则 inc++,否则inc=1 重新开始 2,cnt每次叠加inc 3,最后的返回值 取余数 %