Modify string by rearranging vowels in alphabetical order at their respective indices
Last Updated :
21 Feb, 2023
Given a string S of length N, the task is to sort the vowels of the given string in alphabetical order at place them accordingly in their respective indices.
Examples:
Input: S = “geeksforgeeks”
Output: geeksfergeoks
Explanation:
The vowels in the string are: e, e, o, e, e
Sort in alphabetical order: e, e, e, e, o and replace with the vowels present in original string.
Input: S = “people”
Output: peeplo
Approach: The idea is to store all the vowels present in the string S in another string, say vow. Sort the string vow in alphabetical order. Traverse the string S from the start and replace S[i] with vow[j] if S[i] is a vowel, and incrementing j by 1. Follow the steps below to solve the problem:
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void sortVowels(string S)
{
int n = S.size();
string vow = "" ;
for ( int i = 0; i < n; i++) {
if (S[i] == 'a' || S[i] == 'e'
|| S[i] == 'i' || S[i] == 'o'
|| S[i] == 'u' ) {
vow += S[i];
}
}
if (vow.size() == 0) {
cout << S;
return ;
}
sort(vow.begin(), vow.end());
int j = 0;
for ( int i = 0; i < n; i++) {
if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i'
|| S[i] == 'o' || S[i] == 'u' ) {
S[i] = vow[j++];
}
}
cout << S;
}
int main()
{
string S = "geeksforgeeks" ;
sortVowels(S);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static void sortVowels(String S)
{
int n = S.length();
String vow = "" ;
for ( int i = 0 ; i < n; i++) {
if (S.charAt(i) == 'a' || S.charAt(i) == 'e'
|| S.charAt(i) == 'i' || S.charAt(i) == 'o'
|| S.charAt(i) == 'u' ) {
vow = vow.substring( 0 , vow.length())
+ S.charAt(i);
}
}
if (vow.length() == 0 ) {
System.out.print(S);
return ;
}
char tempArray[] = vow.toCharArray();
Arrays.sort(tempArray);
int j = 0 ;
for ( int i = 0 ; i < n; i++) {
if (S.charAt(i) == 'a' || S.charAt(i) == 'e'
|| S.charAt(i) == 'i' || S.charAt(i) == 'o'
|| S.charAt(i) == 'u' ) {
S = S.substring( 0 , i) + tempArray[j++]
+ S.substring(i + 1 , n);
}
}
System.out.print(S);
}
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
sortVowels(S);
}
}
|
Python3
def sortVowels(S) :
n = len (S);
vow = "";
for i in range (n) :
if (S[i] = = 'a' or S[i] = = 'e'
or S[i] = = 'i' or S[i] = = 'o'
or S[i] = = 'u' ) :
vow + = S[i];
if len (vow) = = 0 :
print (S,end = "");
return ;
vow = list (vow);
vow.sort();
j = 0 ;
for i in range (n) :
if (S[i] = = 'a' or S[i] = = 'e' or S[i] = = 'i'
or S[i] = = 'o' or S[i] = = 'u' ) :
S[i] = vow[j];
j + = 1 ;
print (" ".join(S),end=" ");
if __name__ = = "__main__" :
S = "geeksforgeeks" ;
sortVowels( list (S));
|
C#
using System;
public class GFG
{
static void sortVowels( string S)
{
int n = S.Length;
string vow = "" ;
for ( int i = 0; i < n; i++) {
if (S[i] == 'a' || S[i] == 'e'
|| S[i] == 'i' || S[i] == 'o'
|| S[i] == 'u' ) {
vow = vow.Substring(0, vow.Length)
+ S[i];
}
}
if (vow.Length == 0) {
Console.Write(S);
return ;
}
char []tempArray = vow.ToCharArray();
Array.Sort(tempArray);
int j = 0;
for ( int i = 0; i < n; i++) {
if (S[i] == 'a' || S[i] == 'e'
|| S[i] == 'i' || S[i] == 'o'
|| S[i] == 'u' ) {
S = S.Substring(0, i) + tempArray[j++]
+ S.Substring(i + 1, n - i - 1);
}
}
Console.Write(S);
}
public static void Main( string [] args)
{
string S = "geeksforgeeks" ;
sortVowels(S);
}
}
|
Javascript
<script>
function sortVowels(S)
{
var n = S.length;
var vow = "" ;
for ( var i = 0; i < n; i++) {
if (S[i] == 'a' || S[i] == 'e'
|| S[i] == 'i' || S[i] == 'o'
|| S[i] == 'u' ) {
vow += S[i];
}
}
if (vow.length == 0) {
document.write( S);
return ;
}
vow = vow.split( '' ).sort();
var j = 0;
for ( var i = 0; i < n; i++) {
if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i'
|| S[i] == 'o' || S[i] == 'u' ) {
S[i] = vow[j++];
}
}
document.write( S.join( '' ));
}
var S = "geeksforgeeks" .split( '' );
sortVowels(S);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...