Program to count number of distinct substrings in s in Python

PythonServer Side ProgrammingProgramming

Suppose we have a string s, we have to find the number of distinct non-empty substrings of s.

So, if the input is like s = "abaa", then the output will be 8 because, the substrings are ["a", "b", "ab", "ba", "aa", "aba", "baa", "abaa"].

To solve this, we will follow these steps −

  • trie := a new map
  • n := size of s
  • for i in range 0 to n - 1, do
    • curr := trie
    • for j in range i to n - 1, do
      • c := s[j]
      • if c not in curr, then
        • curr[c] := a new map
      • curr := curr[c]
      • curr["*"] := True
  • q := make a queue, and insert trie
  • ans := 0
  • while q is non-empty, do
    • ans := ans + 1
    • t := left item of q, and delete this item from q
    • for each c in t, do
      • if c is not same as "*", then
        • insert t[c] at the end of q
  • return ans - 1

Example

Let us see the following implementation to get better understanding −

from collections import deque
def solve(s):
   trie = {}
   n = len(s)
   for i in range(n):
      curr = trie
      for j in range(i, n):
         c = s[j]
         if c not in curr:
            curr[c] = {}
         curr = curr[c]
         curr["*"] = True

   q = deque([trie])
   ans = 0
   while q:
      ans += 1
      t = q.popleft()
      for c in t:
         if c != "*":
            q.append(t[c])
   return ans - 1

s = "abaa"
print(solve(s))

Input

"abaa"

Output

8
raja
Published on 18-Oct-2021 12:18:36
Advertisements