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.
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.
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
2
abced
pperu
ebcad
ppure
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".
2
ababa
zyxw
ababa
zyxw
Try to store all the vowels in an auxiliary data structure while maintaining their order.
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:
We are iterating through the String ‘S’ twice. Hence, the overall Time Complexity is O(|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|).