text: Check if a string can be converted to another by swapping of adjacent characters of given type | C # program for the above approach ; 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
code: using System ; class GFG { static bool canTransform ( string str1 , string str2 ) { string s1 = " " ; string s2 = " " ; foreach ( char c in str1 . ToCharArray ( ) ) { if ( c != ' C ' ) { s1 += c ; } } foreach ( char c in str2 . ToCharArray ( ) ) { if ( c != ' C ' ) { s2 += c ; } } if ( s1 != s2 ) return false ; int i = 0 ; int j = 0 ; int n = str1 . Length ; while ( i < n && j < n ) { if ( str1 [ i ] == ' C ' ) { i ++ ; } else if ( str2 [ j ] == ' C ' ) { j ++ ; } else { if ( ( str1 [ i ] == ' A ' && i < j ) || ( str1 [ i ] == ' B ' && i > j ) ) { return false ; } i ++ ; j ++ ; } } return true ; } public static void Main ( string [ ] args ) { string str1 = " BCCABCBCA " ; string str2 = " CBACCBBAC " ; if ( canTransform ( str1 , str2 ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }