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:
- Initialize a string vow to store all the vowels present in the string, S.
- Traverse the string S and check if the current character S[i] is a vowel or not, If found to be true, then push S[i] to vow.
- Sort the string vow in alphabetical order and initialize a variable, say j as 0.
- Again traverse the string S using the variable i and if the current character S[i] is a vowel, then replace S[i] with vow[j] and increment j by 1.
- After the above steps, print the string S as the result.
Below is the implementation of the above approach.
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to arrange the vowels // in sorted order in the string // at their respective places void sortVowels(string S)
{ // Store the size of the string
int n = S.size();
// Stores vowels of string S
string vow = "" ;
// Traverse the string, S and push
// all the vowels to 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 is empty, then print S
// and return
if (vow.size() == 0) {
cout << S;
return ;
}
// Sort vow in alphabetical order
sort(vow.begin(), vow.end());
int j = 0;
// Traverse the string, S
for ( int i = 0; i < n; i++) {
// Replace S[i] with vow[j] if S[i]
// is a vowel, and increment j by 1
if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i'
|| S[i] == 'o' || S[i] == 'u' ) {
S[i] = vow[j++];
}
}
// Print the string
cout << S;
} // Driver Code int main()
{ string S = "geeksforgeeks" ;
sortVowels(S);
return 0;
} |
// Java program for the above approach import java.util.Arrays;
class GFG
{ // Function to arrange the vowels
// in sorted order in the string
// at their respective places
static void sortVowels(String S)
{
// Store the size of the string
int n = S.length();
// Stores vowels of string S
String vow = "" ;
// Traverse the string, S and push
// all the vowels to 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 is empty, then print S
// and return
if (vow.length() == 0 ) {
System.out.print(S);
return ;
}
// Convert vow to char array
char tempArray[] = vow.toCharArray();
// Sort vow in alphabetical order
Arrays.sort(tempArray);
int j = 0 ;
// Traverse the string, S
for ( int i = 0 ; i < n; i++) {
// Replace S[i] with vow[j] if S[i]
// is a vowel, and increment j by 1
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);
}
}
// Print the string
System.out.print(S);
}
// Driver Code
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
sortVowels(S);
}
} // This code is contributed by subhammahato348. |
# Python3 program for the above approach # Function to arrange the vowels # in sorted order in the string # at their respective places def sortVowels(S) :
# Store the size of the string
n = len (S);
# Stores vowels of string S
vow = "";
# Traverse the string, S and push
# all the vowels to string 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 vow is empty, then print S
# and return
if len (vow) = = 0 :
print (S,end = "");
return ;
# Sort vow in alphabetical order
vow = list (vow);
vow.sort();
j = 0 ;
# Traverse the string, S
for i in range (n) :
# Replace S[i] with vow[j] if S[i]
# is a vowel, and increment j by 1
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 the string
print (" ".join(S),end=" ");
# Driver Code if __name__ = = "__main__" :
S = "geeksforgeeks" ;
sortVowels( list (S));
# This code is contributed by AnkThon
|
// C# program for the above approach using System;
public class GFG
{ // Function to arrange the vowels
// in sorted order in the string
// at their respective places
static void sortVowels( string S)
{
// Store the size of the string
int n = S.Length;
// Stores vowels of string S
string vow = "" ;
// Traverse the string, S and push
// all the vowels to 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 is empty, then print S
// and return
if (vow.Length == 0) {
Console.Write(S);
return ;
}
// Convert vow to char array
char []tempArray = vow.ToCharArray();
// Sort vow in alphabetical order
Array.Sort(tempArray);
int j = 0;
// Traverse the string, S
for ( int i = 0; i < n; i++) {
// Replace S[i] with vow[j] if S[i]
// is a vowel, and increment j by 1
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);
}
}
// Print the string
Console.Write(S);
}
// Driver Code
public static void Main( string [] args)
{
string S = "geeksforgeeks" ;
sortVowels(S);
}
} // This code is contributed by AnkThon |
<script> // Javascript program for the above approach // Function to arrange the vowels // in sorted order in the string // at their respective places function sortVowels(S)
{ // Store the size of the string
var n = S.length;
// Stores vowels of string S
var vow = "" ;
// Traverse the string, S and push
// all the vowels to string 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 is empty, then print S
// and return
if (vow.length == 0) {
document.write( S);
return ;
}
// Sort vow in alphabetical order
vow = vow.split( '' ).sort();
var j = 0;
// Traverse the string, S
for ( var i = 0; i < n; i++) {
// Replace S[i] with vow[j] if S[i]
// is a vowel, and increment j by 1
if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i'
|| S[i] == 'o' || S[i] == 'u' ) {
S[i] = vow[j++];
}
}
// Print the string
document.write( S.join( '' ));
} // Driver Code var S = "geeksforgeeks" .split( '' );
sortVowels(S); </script> |
geeksfergeoks
Time Complexity: O(N*log N)
Auxiliary Space: O(N)