- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to count number of homogenous substrings in Python

Suppose we have a string s, we have to find the number of homogenous substrings of s. The answer may be very large, so return answer modulo 10^9+7. A string is said to be homogenous when all the characters of the string are the same.

So, if the input is like s = "xyyzzzxx", then the output will be 13 because the homogenous substrings are listed like

1."x" appears thrice.

"xx" appears once.

3. "y" appears twice.

"yy" appears once.

5. "z" appears thrice.

"zz" appears twice.

"zzz" appears once.

so , (3 + 1 + 2 + 1 + 3 + 2 + 1) = 13.

To solve this, we will follow these steps −

s := s concatenate "@"

h:= a new map

prev:= s[0]

c:= 1

for each i in s from index 1 to end, do

if prev is not same as i, then

if prev*c is present in h, then

h[prev*c] := h[prev*c] + 1

otherwise,

h[prev*c]:= 1

c:= 1

if prev is same as i, then

c := c + 1

prev := i

fin:= 0

for each i in h, do

t:= size of i

k:= 0

while t is not same as 0, do

k := k + t

t := t - 1

fin := fin + k*h[i]

return fin mod 10^9+7

## Example

Let us see the following implementation to get better understanding −

def solve(s): s+="@" h={} prev=s[0] c=1 for i in s[1:]: if prev!=i: if prev*c in h: h[prev*c]+=1 else: h[prev*c]=1 c=1 if prev == i: c += 1 prev = i fin=0 for i in h: t=len(i) k=0 while t!=0: k+=t t-=1 fin+=k*h[i] return fin % 1000000007 s = "xyyzzzxx" print(solve(s))

## Input

"xyyzzzxx"

## Output

13

- Related Questions & Answers
- Program to count number of palindromic substrings in Python
- Program to count number of distinct substrings in s in Python
- Program to count number of similar substrings for each query in Python
- Program to count maximum score from removing substrings in Python
- Program to find maximum number of non-overlapping substrings in Python
- C++ code to count number of even substrings of numeric string
- Program to find number of substrings with only 1s using Python
- Program to count substrings that differ by one character in Python
- Program to find out the number of pairs of equal substrings in Python
- Program to count substrings with all 1s in binary string in Python
- Program to count number of unhappy friends in Python
- Program to count number of nice subarrays in Python
- Count number of substrings with exactly k distinct characters in C++
- Program to find out number of distinct substrings in a given string in python
- Program to find number of different substrings of a string for different queries in Python