Last Updated: 11 Mar, 2021

Reverse vowels in a string

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

Approaches

01 Approach

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

02 Approach

Let us try and find an approach to solve a simplified version of the problem where all the characters of the string ‘S’ are vowels. As all the characters of the string are vowels, the problem simplifies to reverse the string. Let us see an in place algorithm to reverse the string. To solve this problem, we can maintain two pointers, one at each end of the string, using which we will iterate through the string and swap the two characters that are currently being pointed by the two pointers and then move both the pointers forward to carry the next iteration. We will end iterating through the string when both the pointers cross each other. Similarly, consider the case when consonants are also present. 

Now we will again maintain two pointers, one at each end of the string. Now, we will iterate through both the strings and swap the two characters pointed by the two characters only if both of them are vowels. Otherwise, we will move both the pointers ahead separately until both of them start pointing to a vowel. Again we will end iterating through the string when both the pointers cross each other. 

 

Steps: 

  1. Let ‘l’ and ‘r’ be the two pointers to the string ‘S’. Initialize ‘l’ as 0 and ‘r’ as S.length( ) - 1 .
  2. While ‘l’ is smaller than ‘r’
    • If both S[l] and S[r] are vowels, then
      • Swap the two characters  S[l] and S[r]. 
      • Increase ‘l’ by 1 and decrease ‘r’ by 1.
      • Move to the next iteration without executing the below instructions.
    • If S[l] is a consonant, then increase ‘l’ by 1.
    • Otherwise, if S[r] is a consonant, then decrease ‘r’ by 1.
  3. Return the modified string ‘S’.