Generate a String from given Strings P and Q based on the given conditions | C # Program to implement the above approach ; Function to generate a string S from string P and Q according to the given conditions ; Stores the frequencies ; Counts occurrences of each characters ; Reduce the count of the character which is also present in Q ; Stores the resultant string ; Index in [ ] freq to segregate the string ; Add Q to the resulting string ; Driver Code ; Function call
using System ; class GFG { public static void manipulateStrings ( String P , String Q ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < P . Length ; i ++ ) { freq [ P [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < Q . Length ; i ++ ) { freq [ Q [ i ] - ' a ' ] -- ; } String sb = " " ; int pos = Q [ 0 ] - ' a ' ; for ( int i = 0 ; i <= pos ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb += c ; freq [ i ] -- ; } } sb += Q ; for ( int i = pos + 1 ; i < 26 ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb += c ; freq [ i ] -- ; } } Console . WriteLine ( sb ) ; } public static void Main ( String [ ] args ) { String P = " geeksforgeeksfor " ; String Q = " for " ; manipulateStrings ( P , Q ) ; } }