Check if a string is a scrambled form of another string | Javascript program to check if a given string is a scrambled form of another string ; Strings of non - equal length can 't be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Converting string to character array ; Checking condition for Anagram ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; If none of the above conditions are satisfied ; Driver code
function isScramble ( S1 , S2 ) { if ( S1 . length != S2 . length ) { return false ; } let n = S1 . length ; if ( n == 0 ) { return true ; } if ( S1 == S2 ) { return true ; } let tempArray1 = S1 . split ( ' ' ) ; let tempArray2 = S2 . split ( ' ' ) ; tempArray1 . sort ( ) ; tempArray2 . sort ( ) ; let copy_S1 = tempArray1 . join ( " " ) ; let copy_S2 = tempArray2 . join ( " " ) ; if ( copy_S1 != copy_S2 ) { return false ; } for ( let i = 1 ; i < n ; i ++ ) { if ( isScramble ( S1 . substring ( 0 , i ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( i , i + n ) , S2 . substring ( i , i + n ) ) ) { return true ; } if ( isScramble ( S1 . substring ( n - i , n - i + n ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( 0 , n - i ) , S2 . substring ( i , i + n ) ) ) { return true ; } } return false ; } let S1 = " " ; let S2 = " " ; if ( isScramble ( S1 , S2 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }