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