Number of sub | JavaScript program to find the number of sub - Strings of s1 which are anagram of any sub - String of s2 ; This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of String pat [ ] in String txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; cout << pat << " ▁ " << txt << " ▁ " ; ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Function to return the number of sub - Strings of s1 that are anagrams of any sub - String of s2 ; initializing variables ; outer loop for picking starting point ; loop for different length of subStrings ; If s2 has any subString which is anagram of s1 . substr ( i , len ) ; increment the count ; Driver code
let MAX_LEN = 1005 ; let MAX_CHAR = 26 ; let ALL_CHARS = 256 ; function compare ( arr1 , arr2 ) { for ( let i = 0 ; i < ALL_CHARS ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } function search ( pat , txt ) { let M = pat . length ; let N = txt . length ; let i ; let countP = new Array ( ALL_CHARS ) ; countP . fill ( 0 ) ; let countTW = new Array ( ALL_CHARS ) ; countTW . fill ( 0 ) ; for ( i = 0 ; i < M ; i ++ ) { countP [ pat [ i ] . charCodeAt ( ) ] ++ ; countTW [ txt [ i ] . charCodeAt ( ) ] ++ ; } for ( i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) { return true ; } countTW [ txt [ i ] . charCodeAt ( ) ] ++ ; countTW [ txt [ i - M ] . charCodeAt ( ) ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } function calculatesubString ( s1 , s2 , n ) { let count = 0 , j = 0 , x = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let len = 1 ; len <= n - i ; len ++ ) { if ( search ( s1 . substring ( i , i + len ) , s2 ) ) { count = count + 1 ; } } } return count ; } let str1 = " " ; let str2 = " " ; let len = str1 . length ; document . write ( calculatesubString ( str1 , str2 , len ) ) ;