aboutsummaryrefslogtreecommitdiff
path: root/count_num_homogenous_substrings.py
blob: 7d2b7eb39b94a5c75162f864d08bfcb3be06d4ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 1759. Count Number of Homogenous Substrings

"""
given a string 's', return the number of homogenous substrings of 's'. since
thhe answer may be very large, return it modulo 10^9+7. a string is
homogenous if all the characters of the string are the same. a substring is a
continuous sequence of chharacters within a string.
"""


class Solution(object):
    def countHomogenous(self, s):
        ans = 0
        for c, s in groupby(s):
            n = len(list(s))
            ans += n * (n + 1) / 2
        return ans % (10**9 + 7)


if __name__ == "__main__":
    obj = Solution()
    print(obj.countHomogenous("abbcccaa"))  # expect: 13
    print(obj.countHomogenous("xy"))  # expect: 2
    print(obj.countHomogenous("zzzzz"))  # expect: 15