Skip to content
LC-1759 Medium LeetCode

1759. Count Number of Homogenous Substrings

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 57% Topics: Math, String
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def countHomogenous(self, s):
        """
        :type s: str
        :rtype: int
        """
        MOD = 10**9+7
        result = cnt = 0
        for i in xrange(len(s)):
            if i and s[i-1] == s[i]:
                cnt += 1
            else:
                cnt = 1
            result = (result+cnt)%MOD
        return result

Solution from kamyu104/LeetCode-Solutions · MIT