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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string solution ( string s ) { vector < char > vowel , consonant ; for ( auto i : s ) { if ( i == ' a ' i == ' e ' i == ' i ' i == ' o ' i == ' u ' ) { vowel . push_back ( i ) ; } else { consonant . push_back ( i ) ; } } int Nc , Nv ; Nv = vowel . size ( ) ; Nc = consonant . size ( ) ; int M = Nc / ( Nv - 1 ) ; string ans = " " ; int consotnant_till = 0 ; for ( auto i : vowel ) { ans += i ; int temp = 0 ; for ( int j = consotnant_till ; j < min ( Nc , consotnant_till + M ) ; j ++ ) { ans += consonant [ j ] ; temp ++ ; } consotnant_till += temp ; } return ans ; } int main ( ) { string str = " aaaabbbcc " ; cout << solution ( str ) ; return 0 ; }