Open In App

Modify string by rearranging vowels in alphabetical order at their respective indices

Last Updated : 21 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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




// 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




# 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#




// 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


Javascript




<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>


Output: 

geeksfergeoks

 

Time Complexity: O(N*log N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads