Case | C # implementation of the approach ; Function to return the sorted string ; To store the frequencies of the lowercase and the uppercase characters in the given string ; If current character is lowercase then increment its frequency in the lower [ ] array ; Else increment in the upper [ ] array ; Pointers that point to the smallest lowercase and the smallest uppercase characters respectively in the given string ; For every character in the given string ; If the current character is lowercase then replace it with the smallest lowercase character available ; Decrement the frequency of the used character ; Else replace it with the smallest uppercase character available ; Decrement the frequency of the used character ; Return the sorted string ; Driver code
using System ; class GFG { static int MAX = 26 ; static string getSortedString ( char [ ] s , int n ) { int [ ] lower = new int [ MAX ] ; int [ ] upper = new int [ MAX ] ; int i = 0 , j = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( char . IsLower ( s [ i ] ) ) lower [ s [ i ] - ' a ' ] ++ ; else if ( char . IsUpper ( s [ i ] ) ) upper [ s [ i ] - ' A ' ] ++ ; } i = 0 ; while ( i < MAX && lower [ i ] == 0 ) i ++ ; while ( j < MAX && upper [ j ] == 0 ) j ++ ; for ( int k = 0 ; k < n ; k ++ ) { if ( char . IsLower ( s [ k ] ) ) { while ( lower [ i ] == 0 ) i ++ ; s [ k ] = ( char ) ( i + ' a ' ) ; lower [ i ] -- ; } else if ( char . IsUpper ( s [ k ] ) ) { while ( upper [ j ] == 0 ) j ++ ; s [ k ] = ( char ) ( j + ' A ' ) ; upper [ j ] -- ; } } return String . Join ( " " , s ) ; } public static void Main ( String [ ] args ) { String s = " gEeksfOrgEEkS " ; int n = s . Length ; Console . WriteLine ( getSortedString ( s . ToCharArray ( ) , n ) ) ; } }