Check if count of substrings in S with string S1 as prefix and S2 as suffix is equal to that with S2 as prefix and S1 as suffix | Function to count number of substrings that starts with string S1 and ends with string S2 ; Stores the length of each string ; Stores the count of prefixes as S1 and suffixes as S2 ; Traverse string S ; Find the prefix at index i ; Find the suffix at index i ; If the prefix is S1 ; If the suffix is S2 ; Return the count of substrings ; Function to check if the number of substrings starts with S1 and ends with S2 and vice - versa is same or not ; Count the number of substrings ; Print the result ; Driver Code
function countSubstrings ( S , S1 , S2 ) { let N = S . length ; let N1 = S1 . length ; let N2 = S2 . length ; let count = 0 , totalcount = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let prefix = S . substr ( i , N1 ) ; let suffix = S . substr ( i , N2 ) ; if ( S1 == prefix ) count ++ ; if ( S2 == suffix ) totalcount += count ; } return totalcount ; } function checkSubstrings ( S , S1 , S2 ) { let x = countSubstrings ( S , S1 , S2 ) ; let y = countSubstrings ( S , S2 , S1 ) ; if ( x == y ) document . write ( " " ) ; else document . write ( " " ) ; } let S = " " ; let S1 = " " ; let S2 = " " ; checkSubstrings ( S , S1 , S2 ) ;