Sort a string without altering the position of vowels
Last Updated :
28 May, 2021
Given a string S of size N, the task is to sort the string without changing the position of vowels.
Examples:
Input: S = “geeksforgeeks”
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:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortStr(string S)
{
int N = S.size();
string temp = "" ;
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];
}
if (temp.size())
sort(temp.begin(), temp.end());
int ptr = 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] = temp[ptr++];
}
cout << S;
}
int main()
{
string S = "geeksforgeeks" ;
sortStr(S);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void sortStr(String str)
{
char S[] = str.toCharArray();
int N = S.length;
ArrayList<Character> temp = new ArrayList<>();
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]);
}
if (temp.size() != 0 )
Collections.sort(temp);
int ptr = 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] = temp.get(ptr++);
}
System.out.println( new String(S));
}
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
sortStr(S);
}
}
|
Python3
def sortStr(S):
N = len (S)
temp = ""
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]
if ( len (temp)):
p = list (temp)
p.sort()
temp = ''.join(p)
ptr = 0
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))
if __name__ = = "__main__" :
S = "geeksforgeeks"
sortStr(S)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static void sortStr(String str)
{
char []S = str.ToCharArray();
int N = S.Length;
List< char > temp = new List< char >();
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]);
}
if (temp.Count != 0)
temp.Sort();
int ptr = 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] = temp[ptr++];
}
Console.WriteLine( new String(S));
}
public static void Main(String[] args)
{
String S = "geeksforgeeks" ;
sortStr(S);
}
}
|
Javascript
<script>
function sortStr(str)
{
var S = str.split( '' );
var N = S.length;
var temp = [];
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]);
}
if (temp.length != 0)
temp.sort();
var ptr = 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] = temp[ptr++];
}
var str = "" ;
for ( var i =0;i<S.length;i++)
str+=S[i];
document.write(str);
}
var S = "geeksforgeeks" ;
sortStr(S);
</script>
|
Time Complexity: O(N logN)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...