Rearrange a string to maximize the minimum distance between any pair of vowels | 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 consonant ; store count of vowels and consonants respectively ; store the resultant string ; store count of consonants append into ans ; Append vowel to ans ; Append consonants ; Appendconsonant to ans ; update temp ; Remove the taken elements of consonant ; return final answer ; Driver code ; Function Call
def solution ( S ) : NEW_LINE INDENT vowels = [ ] NEW_LINE consonants = [ ] NEW_LINE for i in S : NEW_LINE INDENT if ( i == ' a ' or i == ' e ' or i == ' i ' or i == ' o ' or i == ' u ' ) : NEW_LINE INDENT vowels . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT consonants . append ( i ) NEW_LINE DEDENT DEDENT Nc = len ( consonants ) NEW_LINE Nv = len ( vowels ) NEW_LINE M = Nc // ( Nv - 1 ) NEW_LINE ans = " " NEW_LINE consonant_till = 0 NEW_LINE for i in vowels : NEW_LINE INDENT ans += i NEW_LINE temp = 0 NEW_LINE for j in range ( consonant_till , min ( Nc , consonant_till + M ) ) : NEW_LINE INDENT ans += consonants [ j ] NEW_LINE temp += 1 NEW_LINE DEDENT consonant_till += temp NEW_LINE DEDENT return ans NEW_LINE DEDENT S = " aaaabbbcc " NEW_LINE print ( solution ( S ) ) NEW_LINE