
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- 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 count substrings that differ by one character in Python
- Program to count substrings with all 1s in binary string in Python
- Program to find maximum number of non-overlapping substrings in Python
- Program to find out the number of pairs of equal substrings in Python
- Program to find number of substrings with only 1s using Python
- C++ code to count number of even substrings of numeric string
- Program to count number of unhappy friends in Python
- Program to count number of nice subarrays in Python
- Program to find out number of distinct substrings in a given string in python
- Golang program to splitting into number of substrings
- Program to find number of different substrings of a string for different queries in Python
