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-2021
https://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,最后的返回值 取余数 %
本文介绍了如何解决 LeetCode 上的一道题目——计数同质子串。作者提供了一个 Python 解决方案,通过遍历字符串并记录相同字符连续出现的次数来计算同质子串的数量。代码简洁高效,适合对字符串处理和算法感兴趣的读者学习。

被折叠的 条评论
为什么被折叠?



