Sort a string without altering the position of vowels

Given a string S of size N, the task is to sort the string without changing the position of vowels.
Examples:
Input: S = “zambiatek”
Output: feeggkokreess
Explanation:
The consonants present in the string are gksfrgks. Sorting the consonants modifies their sequence to fggkkrss.
Now, update the string by placing the sorted consonants in those positions.Input: S = “apple”
Output: alppe
Approach: Follow the steps below to solve the problem:
- Initialize a string, say temp.
- Iterate over the characters of the string S.
- If the current character is a consonant, insert the character into temp.
- Sort the string temp in lexicographical order.
- Initialize a pointer, say ptr = 0, to point to the current character in string temp.
- Now, traverse the string S, and replace each consonant of string S with the temp[ptr]. Increment ptr.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to sort the string// leaving the vowels unchangedvoid sortStr(string S){ // Length of string S int N = S.size(); string temp = ""; // Traverse the string S for (int i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') temp += S[i]; } // Sort the string temp if (temp.size()) sort(temp.begin(), temp.end()); // Pointer to traverse the // sorted string of consonants int ptr = 0; // Traverse the string S 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] = temp[ptr++]; } cout << S;}// Driver Codeint main(){ string S = "zambiatek"; sortStr(S); return 0;} |
Java
// Java program for the above approachimport java.io.*;import java.lang.*;import java.util.*;class GFG{// Function to sort the string// leaving the vowels unchangedstatic void sortStr(String str){ char S[] = str.toCharArray(); // Length of string S int N = S.length; ArrayList<Character> temp = new ArrayList<>(); // Traverse the string S for(int i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') temp.add(S[i]); } // Sort the string temp if (temp.size() != 0) Collections.sort(temp); // Pointer to traverse the // sorted string of consonants int ptr = 0; // Traverse the string S 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] = temp.get(ptr++); } System.out.println(new String(S));}// Driver Codepublic static void main(String[] args){ String S = "zambiatek"; sortStr(S);}}// This code is contributed by Kingash |
Python3
# Python3 program for the above approach# Function to sort the string# leaving the vowels unchangeddef sortStr(S): # Length of string S N = len(S) temp = "" # Traverse the string S for i in range(N): if (S[i] != 'a' and S[i] != 'e' and S[i] != 'i' and S[i] != 'o'and S[i] != 'u'): temp += S[i] # Sort the string temp if (len(temp)): p = list(temp) p.sort() temp=''.join(p) # Pointer to traverse the # sorted string of consonants ptr = 0 # Traverse the string S for i in range(N): S = list(S) if (S[i] != 'a' and S[i] != 'e' and S[i] != 'i' and S[i] != 'o' and S[i] != 'u'): S[i] = temp[ptr] ptr += 1 print(''.join(S))# Driver Codeif __name__ == "__main__": S = "zambiatek" sortStr(S) # This code is contributed by ukasp. |
C#
// C# program for the above approachusing System;using System.Collections.Generic;public class GFG{// Function to sort the string// leaving the vowels unchangedstatic void sortStr(String str){ char []S = str.ToCharArray(); // Length of string S int N = S.Length; List<char> temp = new List<char>(); // Traverse the string S for(int i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') temp.Add(S[i]); } // Sort the string temp if (temp.Count != 0) temp.Sort(); // Pointer to traverse the // sorted string of consonants int ptr = 0; // Traverse the string S 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] = temp[ptr++]; } Console.WriteLine(new String(S));}// Driver Codepublic static void Main(String[] args){ String S = "zambiatek"; sortStr(S);}}// This code is contributed by Amit Katiyar |
Javascript
<script>// JavaScript program for the above approach// Function to sort the string// leaving the vowels unchangedfunction sortStr(str){ var S = str.split(''); // Length of string S var N = S.length; var temp = []; // Traverse the string S for(var i = 0; i < N; i++) { if (S[i] != 'a' && S[i] != 'e' && S[i] != 'i' && S[i] != 'o' && S[i] != 'u') temp.push(S[i]); } // Sort the string temp if (temp.length != 0) temp.sort(); // Pointer to traverse the // sorted string of consonants var ptr = 0; // Traverse the string S 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] = temp[ptr++]; } var str = ""; for(var i =0;i<S.length;i++) str+=S[i]; document.write(str);}// Driver Code var S = "zambiatek"; sortStr(S);// This code is contributed by Amit Katiyar</script> |
Output:
feeggkokreess
Time Complexity: O(N logN)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



