Check if given string can be formed by two other strings or their permutations | C # implementation of the above approach ; Function to sort the given string using counting sort ; Array to store the count of each character ; Insert characters in the string in increasing order ; Function that returns true if str can be generated from any permutation of the two strings selected from the given vector ; Sort the given string ; Select two strings at a time from given vector ; Get the concatenated string ; Sort the resultant string ; If the resultant string is equal to the given string str ; No valid pair found ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 26 ; static String countingsort ( char [ ] s ) { int [ ] count = new int [ MAX ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { count [ s [ i ] - ' a ' ] ++ ; } int index = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { int j = 0 ; while ( j < count [ i ] ) { s [ index ++ ] = ( char ) ( i + ' a ' ) ; j ++ ; } } return String . Join ( " " , s ) ; } static Boolean isPossible ( List < String > v , String str ) { str = countingsort ( str . ToCharArray ( ) ) ; for ( int i = 0 ; i < v . Count - 1 ; i ++ ) { for ( int j = i + 1 ; j < v . Count ; j ++ ) { String temp = v [ i ] + v [ j ] ; temp = countingsort ( temp . ToCharArray ( ) ) ; if ( temp . Equals ( str ) ) { return true ; } } } return false ; } public static void Main ( String [ ] args ) { String str = " amazon " ; String [ ] arr = { " fds " , " oxq " , " zoa " , " epw " , " amn " } ; List < String > v = new List < String > ( arr ) ; if ( isPossible ( v , str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }