Count unique substrings of a string S present in a wraparound string | 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
function findSubstringInWraproundString ( p ) { let ans = 0 ; let curLen = 0 ; let arr = new Array ( 26 ) ; arr . fill ( 0 ) ; for ( let i = 0 ; i < p . length ; i ++ ) { let curr = p [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( i > 0 && ( p [ i - 1 ] . charCodeAt ( ) != ( ( curr + 26 - 1 ) % 26 + ' ' . charCodeAt ( ) ) ) ) { curLen = 0 ; } curLen ++ ; if ( curLen > arr [ curr ] ) { ans += ( curLen - arr [ curr ] ) ; arr [ curr ] = curLen ; } } document . write ( ans ) ; } let p = " " ; static void Main ( ) { string p = " " ; findSubstringInWraproundString ( p ) ;