Check if the given string is shuffled substring of another string | This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of pat [ ] in 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 [ ] ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Driver code
import java . util . * ; class GFG { static boolean compare ( int [ ] arr1 , int [ ] arr2 ) { for ( int i = 0 ; i < 256 ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } static boolean search ( String pat , String txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; int [ ] countP = new int [ 256 ] ; int [ ] countTW = new int [ 256 ] ; for ( int i = 0 ; i < 256 ; i ++ ) { countP [ i ] = 0 ; countTW [ i ] = 0 ; } for ( int i = 0 ; i < M ; i ++ ) { ( countP [ pat . charAt ( i ) ] ) ++ ; ( countTW [ txt . charAt ( i ) ] ) ++ ; } for ( int i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) return true ; ( countTW [ txt . charAt ( i ) ] ) ++ ; countTW [ txt . charAt ( i - M ) ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } public static void main ( String [ ] args ) { String txt = " BACDGABCDA " ; String pat = " ABCD " ; if ( search ( pat , txt ) ) System . out . println ( " Yes " ) ; else System . out . println ( " NO " ) ; } }