Number of sub | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ALL_CHARS  256 NEW_LINE bool compare ( char * arr1 , char * arr2 ) { for ( int i = 0 ; i < ALL_CHARS ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } bool search ( string pat , string txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; int i ; char countP [ ALL_CHARS ] = { 0 } ; char countTW [ ALL_CHARS ] = { 0 } ; for ( i = 0 ; i < M ; i ++ ) { ( countP [ pat [ i ] ] ) ++ ; ( countTW [ txt [ i ] ] ) ++ ; } for ( i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) { return true ; } ( countTW [ txt [ i ] ] ) ++ ; countTW [ txt [ i - M ] ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } int calculatesubString ( string s1 , string s2 , int n ) { int count = 0 , j = 0 , x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = 1 ; len <= n - i ; len ++ ) { if ( search ( s1 . substr ( i , len ) , s2 ) ) { count = count + 1 ; } } } return count ; } int main ( ) { string str1 = " PLEASEHELPIMTRAPPED " ; string str2 = " INAKICKSTARTFACTORY " ; int len = str1 . length ( ) ; cout << calculatesubString ( str1 , str2 , len ) ; return 0 ; }