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 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
function solution ( s ) { var vowel = [ ] , consonant = [ ] ; s . split ( ' ' ) . forEach ( i => { if ( i == ' ' i == ' ' i == ' ' i == ' ' i == ' ' ) { vowel . push ( i ) ; } else { consonant . push ( i ) ; } } ) ; var Nc , Nv ; Nv = vowel . length ; Nc = consonant . length ; var M = parseInt ( Nc / ( Nv - 1 ) ) ; var ans = " " ; var consotnant_till = 0 ; vowel . forEach ( i => { ans += i ; var temp = 0 ; for ( var j = consotnant_till ; j < Math . min ( Nc , consotnant_till + M ) ; j ++ ) { ans += consonant [ j ] ; temp ++ ; } consotnant_till += temp ; } ) ; return ans ; } var str = " " ; document . write ( solution ( str ) ) ;