LC.1180. Count Substrings with Only One Distinct Letter

LC.1180. Count Substrings with Only One Distinct Letter

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
上一篇:Win10系统隐藏文件资源管理器中的3D对象等七个文件夹


下一篇:博维数孪CreateTwins—帮助文档—画布