Check if a string can be converted to another by swapping of adjacent characters of given type | Function to check if it is possible to transform start to end ; Check the sequence of A , B in both Strings str1 and str2 ; If both the Strings are not equal ; Traverse the Strings ; Check for indexes of A and B ; Driver Code ; Function call
function canTransform ( str1 , str2 ) { var s1 = " " ; var s2 = " " ; for ( const c of str1 ) { if ( c !== " " ) { s1 += c ; } } for ( const c of str2 ) { if ( c !== " " ) { s2 += c ; } } if ( s1 !== s2 ) return false ; var i = 0 ; var j = 0 ; var n = str1 . length ; while ( i < n && j < n ) { if ( str1 [ i ] === " " ) { i ++ ; } else if ( str2 [ j ] === " " ) { j ++ ; } else { if ( ( str1 [ i ] === " " && i < j ) || ( str1 [ i ] === " " && i > j ) ) { return false ; } i ++ ; j ++ ; } } return true ; } var str1 = " " ; var str2 = " " ; if ( canTransform ( str1 . split ( " " ) , str2 . split ( " " ) ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }