Program to count number of homogenous substrings in Python

PythonServer Side ProgrammingProgramming

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
raja
Published on 06-Oct-2021 08:32:27
Advertisements