Rearrange a string to maximize the minimum distance between any pair of vowels | C # program for the above approach ; Function to rearrange the String such that the minimum distance between any of vowels is maximum . ; Store vowels and consonants ; Iterate over the characters of String ; If current character is a vowel ; If current character is a consonant ; Stores count of vowels and consonants respectively ; Stores the resultant String ; Stores count of consonants appended into ans ; Append vowel to ans ; Append consonants ; Append consonant to ans ; Update temp ; Remove the taken elements of consonant ; Return final ans ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static String solution ( string s ) { List < char > vowel = new List < char > ( ) ; List < char > consonant = new List < char > ( ) ; foreach ( char i in s . ToCharArray ( ) ) { if ( i == ' a ' i == ' e ' i == ' i ' i == ' o ' i == ' u ' ) { vowel . Add ( i ) ; } else { consonant . Add ( i ) ; } } int Nc , Nv ; Nv = vowel . Count ; Nc = consonant . Count ; int M = Nc / ( Nv - 1 ) ; string ans = " " ; int consotnant_till = 0 ; foreach ( char i in vowel ) { ans += i ; int temp = 0 ; for ( int j = consotnant_till ; j < Math . Min ( Nc , consotnant_till + M ) ; j ++ ) { ans += consonant [ j ] ; temp ++ ; } consotnant_till += temp ; } return ans ; } static public void Main ( ) { String str = " aaaabbbcc " ; Console . WriteLine ( solution ( str ) ) ; } }