Reverse vowels in a string

Easy
0/40
Average time to solve is 15m
profile
Contributed by
25 upvotes
Asked in companies
AppleWalmartFlipkart limited

Problem statement

You are given a string 'S'. Your task is to reverse the vowels in the string while keeping the consonants unchanged.

Note:

The alphabets 'a', 'e', 'i', 'o', and 'u' are vowels, while all the remaining alphabets are consonants.
Detailed explanation ( Input/output format, Notes, Images )
Input Format
The first line of input contains an integer 'T' representing the number of test cases.

The first and the only line of each test case contains the string 'S'.
Output Format:
For each test case, return the string after reversing all its vowels. 
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= |S| <= 10^5 

All characters of string S are lowercase alphabets only.

Where '|S|' denotes the length of string 'S'.

Time limit: 1 sec
Sample Input 1:
2
abced
pperu
Sample Output 1:
ebcad
ppure
Explanation for Sample Input 1:
For the first test case, the vowels 'a' and 'e' are present in the string. The resulting string formed after reversing the vowels is "ebcad".

For the second test case, the vowels 'e' and 'u' are present in the string. The resulting string formed after reversing the vowels is "ppure".
Sample Input 2:
2
ababa
zyxw
Sample Output 2:
ababa
zyxw
Hint

Try to store all the vowels in an auxiliary data structure while maintaining their order.

Approaches (2)
Solution Using Linear Extra Space

The idea is to store all the vowels present in the string in another string while maintaining their respective order. After storing all the vowels in a string, we will reverse that string so that the order of all the vowels gets reversed. After that, we will iterate through the original string and replace all the vowels in the string in the order that they appear in the other string while keeping the consonants unchanged. 

 

Steps: 

  1. Let ‘VOWELS’ be a string that store all the vowels present in string ‘S’.
  2. Iterate through ‘i’ = 0 to S.length( ) - 1 
    • If S[i] is a vowel, then add S[i] to the end of string ‘VOWELS’.
  3. Reverse the string ‘VOWELS’ so that the order of all the vowels gets reversed.
  4. Let ‘j’ be a pointer to the string ‘VOWELS’. Initialize ‘j’ as 0.
  5. Iterate through ‘i’ = 0 to S.length( ) - 1
    • If S[i] is a vowel, then replace S[i] with VOWELS[j] and increase ‘j’ by 1.
  6. Return the modified string ‘S’.
Time Complexity

O(|S|), where |S| denotes the length of the string ‘S’.

 

We are iterating through the String ‘S’ twice. Hence, the overall Time Complexity is O(|S|).

Space Complexity

O(|S|), where |S| denotes the length of the string ‘S’.

 

In the worst case, when all the characters of the String ‘S’ are vowels, the length of string vowels will be equal to the length of string ‘S’. Hence, the overall Space Complexity is O(|S|).

Code Solution
(100% EXP penalty)
Reverse vowels in a string
Full screen
Console