Rearrange a string to maximize the minimum distance between any pair of vowels | Java 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
import java . util . * ; class GFG { static String solution ( String s ) { Vector < Character > vowel = new Vector < Character > ( ) ; Vector < Character > consonant = new Vector < Character > ( ) ; for ( char i : 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 . size ( ) ; Nc = consonant . size ( ) ; int M = Nc / ( Nv - 1 ) ; String ans = " " ; int consotnant_till = 0 ; for ( char i : vowel ) { ans += i ; int temp = 0 ; for ( int j = consotnant_till ; j < Math . min ( Nc , consotnant_till + M ) ; j ++ ) { ans += consonant . get ( j ) ; temp ++ ; } consotnant_till += temp ; } return ans ; } public static void main ( String [ ] args ) { String str = " aaaabbbcc " ; System . out . print ( solution ( str ) ) ; } }