class Solution:
def countLetters(self, S: str) -> int:
"""
统计连续相同字符出现的次数即可
对于长度为n 的相同字符的字符串,他有 1+2+..n个子串
"""
def sum_n(n):
return (n+1)*n //2
last_char, result, count, S = "$", 0, 0, S +"$"
for char in S:
if char == last_char:
count += 1
else:
result += sum_n(count)
count = 1
last_char = char
return result