Rearrange a string to maximize the minimum distance between any pair of vowels
Last Updated :
28 May, 2021
Given a string str, the task is to rearrange the characters of the given string such that the minimum distance between any pair of vowels is maximum possible.
Examples:
Input: str = “aacbbc”
Output: abcbca
Explanation: Maximized distance between the only pair of vowels is 4.
Input: str = “aaaabbbcc”
Output: ababacbac
Approach: Follow the below steps to solve the problem:
- Iterate over the characters of the string and count the number of vowels and consonants present in the string str, say Nv and Nc respectively.
- Now, calculate the maximum number of consonants that can be put between every pair of vowels using the following formula:
M = rounded down[Nc / (Nv – 1)]
- Now, construct the resultant string by placing all vowels and then inserting M consonants between each adjacent vowel of the pair.
- After constructing the resultant string, if any consonant is still remaining, then simply insert them at the end of the string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def solution(S):
vowels = []
consonants = []
for i in S:
if (i = = 'a' or i = = 'e' or i = = 'i' or i = = 'o' or i = = 'u' ):
vowels.append(i)
else :
consonants.append(i)
Nc = len (consonants)
Nv = len (vowels)
M = Nc / / (Nv - 1 )
ans = ""
consonant_till = 0
for i in vowels:
ans + = i
temp = 0
for j in range (consonant_till, min (Nc, consonant_till + M)):
ans + = consonants[j]
temp + = 1
consonant_till + = temp
return ans
S = "aaaabbbcc"
print (solution(S))
|
C#
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));
}
}
|
Javascript
<script>
function solution(s)
{
var vowel = [], consonant = [];
s.split( '' ).forEach(i => {
if (i == 'a' || i == 'e'
|| i == 'i' || i == 'o'
|| i == 'u' ) {
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 = "aaaabbbcc" ;
document.write( solution(str));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...