text: Check if a string can be transformed to another by sorting substrings | C # program to implement the above approach ; Function to check if str1 can be transformed to t by sorting subStrings ; Occur [ i ] stores the indices of char ( ' a ' + i ) in String s ; idx [ i ] stores the next available index of char ( ' a ' + i ) in occur [ i ] ; If this char is not available anymore ; Conversion not possible ; If one of the smaller characters is available and occurs before ; Conversion not possible ; Print the answer ; Driver Code
code: using System ; using System . Collections . Generic ; class GFG { static void canTransform ( String s , String t ) { int n = s . Length ; List < int > [ ] occur = new List < int > [ 26 ] ; for ( int i = 0 ; i < occur . Length ; i ++ ) occur [ i ] = new List < int > ( ) ; for ( int x = 0 ; x < n ; x ++ ) { char ch = ( char ) ( s [ x ] - ' a ' ) ; occur [ ch ] . Add ( x ) ; } int [ ] idx = new int [ 26 ] ; bool poss = true ; for ( int x = 0 ; x < n ; x ++ ) { char ch = ( char ) ( t [ x ] - ' a ' ) ; if ( idx [ ch ] >= occur [ ch ] . Count ) { poss = false ; break ; } for ( int small = 0 ; small < ch ; small ++ ) { if ( idx [ small ] < occur [ small ] . Count && occur [ small ] [ idx [ small ] ] < occur [ ch ] [ idx [ ch ] ] ) { poss = false ; break ; } } idx [ ch ] ++ ; } if ( poss ) { Console . Write ( " Yes " + " STRNEWLINE " ) ; } else { Console . Write ( " No " + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { String s , t ; s = " hdecb " ; t = " cdheb " ; canTransform ( s , t ) ; } }