Program to count number of distinct characters of every substring of a string in Python

PythonServer Side ProgrammingProgramming

Suppose we have a lowercase string s, we have to find the sum of the count of characters that are distinct in every substring of s. If the answer is very large then return result mod 10^9+7.

So, if the input is like s = "xxy", then the output will be 6, as the substrings and their counts are −

  • "x" : 1

  • "x" : 1

  • "y" : 1

  • "xx" : 0 (as "x" is not distinct)

  • "xy" : 2

  • "xxy" : 1 ("as "x" is not distinct)

To solve this, we will follow these steps −

  • m := 10^9 + 7

  • prev_seen := a new empty map

  • ans := 0

  • Define a function util() . This will take i, symbol

  • prev_seen[symbol] := a list with single value −1

  • prev := prev_seen[symbol]

  • insert i at the end of prev

  • if size of prev > 2, then

    • left := first element of prev and delete first element from prev

    • middle := prev[0], right := prev[1]

    • cnt :=(middle − left) *(right − middle)

    • ans :=(ans + cnt) mod m

  • for each index i and value symbol in s, do

    • util(i, symbol)

  • for each symbol in prev_seen, do

    • util(size of s , symbol)

  • return ans

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, s):
      m = 10 ** 9 + 7
      prev_seen = {}
      ans = 0
      def util(i, symbol):
         nonlocal ans
         prev = prev_seen.setdefault(symbol, [−1])
         prev.append(i)
         if len(prev) > 2:
            left = prev.pop(0)
            middle, right = prev
            cnt = (middle − left) * (right − middle)
            ans = (ans + cnt) % m
      for i, symbol in enumerate(s):
         util(i, symbol)
      for symbol in prev_seen:
         util(len(s), symbol)
      return ans
ob = Solution()
s = "xxy"
print(ob.solve(s))

Input

xxy

Output

6
raja
Published on 25-Dec-2020 06:01:23
Advertisements