Count unique substrings of a string S present in a wraparound string

• Difficulty Level : Medium
• Last Updated : 26 Apr, 2021

Given a string S which is an infinite wraparound string of the string “abcdefghijklmnopqrstuvwxyz”, the task is to count the number of unique non-empty substrings of a string p are present in s.

Examples:

Input: S = “zab”
Output: 6
Explanation: All possible substrings are “z”, “a”, “b”, “za”, “ab”, “zab”.

Input: S = “cac”
Output: 2
Explanation: All possible substrings are “a” and “c” only.

Approach: Follow the steps below to solve the problem

1. Iterate over each character of the string
2. Initialize an auxiliary array arr[] of size 26, to store the current length of substring that is present in string S starting from each character of string P.
3. Initialize a variable, say curLen, which stores the length of substring present in P including the current character if the current character is not a part of the previous substring.
4. Initialize a variable, say ans, to store the unique count of non-empty substrings of p present in S.
5. Iterate over the characters of the string and check for the following two cases:
• Check if the current character can be added with previous substring to form the required substring or not.
• Add the difference of curLen and arr[curr] to ans if (curLen + 1) is greater than arr[curr] to avoid repetition of substrings.
6. Print the value of ans.

Below is the implementation of the above approach:

C++

 // C++ program for// the above approach #include using namespace std; // Function to find the count of// non-empty substrings of p present in sint findSubstringInWraproundString(string p){    // Stores the required answer    int ans = 0;     // Stores the length of    // substring present in p    int curLen = 0;     // Stores the current length    // of substring that is    // present in string s starting    // from each character of p    int arr = { 0 };     // Iterate over the characters of the string    for (int i = 0; i < (int)p.length(); i++) {         int curr = p[i] - 'a';         // Check if the current character        // can be added with previous substring        // to form the required substring        if (i > 0            && (p[i - 1]                != ((curr + 26 - 1) % 26 + 'a'))) {            curLen = 0;        }         // Increment current length        curLen++;         if (curLen > arr[curr]) {             // To avoid repetition            ans += (curLen - arr[curr]);             // Update arr[cur]            arr[curr] = curLen;        }    }     // Print the answer    cout << ans;} // Driver Codeint main(){    string p = "zab";     // Function call to find the    // count of non-empty substrings    // of p present in s    findSubstringInWraproundString(p);     return 0;}

Java

 import java.util.*;class GFG{       // Function to find the count of    // non-empty substrings of p present in s    static void findSubstringInWraproundString(String p)    {               // Stores the required answer        int ans = 0;         // Stores the length of        // substring present in p        int curLen = 0;         // Stores the current length        // of substring that is        // present in string s starting        // from each character of p        int arr[] = new int;         // Iterate over the characters of the string        for (int i = 0; i < p.length(); i++)        {             int curr = p.charAt(i) - 'a';             // Check if the current character            // can be added with previous substring            // to form the required substring            if (i > 0                && (p.charAt(i - 1)                    != ((curr + 26 - 1) % 26 + 'a')))            {                curLen = 0;            }             // Increment current length            curLen++;            if (curLen > arr[curr])            {                 // To avoid repetition                ans += (curLen - arr[curr]);                 // Update arr[cur]                arr[curr] = curLen;            }        }         // Print the answer        System.out.println(ans);    }     // Driver Code    public static void main(String args[])    {        String p = "zab";         // Function call to find the        // count of non-empty substrings        // of p present in s        findSubstringInWraproundString(p);    }} // This code is contributed by hemanth gadarla

Python3

 # Python3 program for# the above approach # Function to find the count of# non-empty substrings of p present in sdef findSubstringInWraproundString(p) :     # Stores the required answer    ans = 0      # Stores the length of    # substring present in p    curLen = 0      # Stores the current length    # of substring that is    # present in string s starting    # from each character of p    arr = *26      # Iterate over the characters of the string    for i in range(0, len(p)) :          curr = ord(p[i]) - ord('a')          # Check if the current character        # can be added with previous substring        # to form the required substring        if (i > 0 and (ord(p[i - 1]) != ((curr + 26 - 1) % 26 + ord('a')))) :            curLen = 0          # Increment current length        curLen += 1          if (curLen > arr[curr]) :              # To avoid repetition            ans += (curLen - arr[curr])              # Update arr[cur]            arr[curr] = curLen      # Print the answer    print(ans)     p = "zab"  # Function call to find the# count of non-empty substrings# of p present in sfindSubstringInWraproundString(p) # This code is contributed by divyeshrabadiya07.

C#

 // C# program for// the above approachusing System;class GFG{   // Function to find the count of  // non-empty substrings of p present in s  static void findSubstringInWraproundString(string p)  {         // Stores the required answer    int ans = 0;     // Stores the length of    // substring present in p    int curLen = 0;     // Stores the current length    // of substring that is    // present in string s starting    // from each character of p    int[] arr = new int;     // Iterate over the characters of the string    for (int i = 0; i < (int)p.Length; i++)    {      int curr = p[i] - 'a';       // Check if the current character      // can be added with previous substring      // to form the required substring      if (i > 0 && (p[i - 1] != ((curr + 26 - 1) % 26 + 'a')))      {        curLen = 0;      }       // Increment current length      curLen++;      if (curLen > arr[curr])      {         // To avoid repetition        ans += (curLen - arr[curr]);         // Update arr[cur]        arr[curr] = curLen;      }    }     // Print the answer    Console.Write(ans);  }   // Driver code  static void Main()  {    string p = "zab";     // Function call to find the    // count of non-empty substrings    // of p present in s    findSubstringInWraproundString(p);  }} // This code is contributed by divyesh072019.

Javascript


Output:
6

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up