Write the Python Program to Reverse the Vowels in the Given String

In this tutorial, we will write the Python program to reverse the vowel in the given string. This is a common question from string which is asked in the interviews. Let's understand the problem statement.

Problem Statement

We are given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

Example 1:

Input: s = "hello"

Output: "holle"

Example 2:

Input: s = "leetcode"

Output: "leotcede"

We can solve this problem using the various approaches.

Solution - 1:

Let's see the following solution -

Example -

Output:

JivoTpaant

Explanation -

In the above code, we define a function reverseVowels that takes a string s as input and returns the string with its vowels reversed. Here's how the code works:

  1. The input string s is converted into a list of characters using the list() function, allowing us to modify individual characters later on.
  2. Two variables i and j are initialized. i represents the index of the first character in s, and j represents the index of the last character in s.
  3. The code enters a while loop that continues as long as i is less than j.
  4. Inside the loop, there are several conditional statements to handle different cases:
  5. If the character at index i is a lowercase or uppercase vowel (s[i] in ['a','e','i','o','u', 'A','E','I','O','U']) and the character at index j is an uppercase vowel (s[j] in ['A','E','I','O','U']), then the characters at positions i and j are swapped using tuple assignment (s[i],s[j]=s[j],s[i]).
  6. If the character at index i is not a vowel and the character at index j is a vowel, i is incremented (i = i+1) to move to the next character from the beginning of the string.
  7. If the character at index i is a vowel and the character at index j is not a vowel, j is decremented (j = j-1) to move to the next character from the end of the string.
  8. If none of the above conditions is met, both i and j are updated (i=i+1 and j = j-1) to continue searching for vowels.
  9. Once the while loop finishes, the modified list s is converted back to a string using the "".join(s) method, where an empty string is used as the separator between characters.
  10. The resulting string is returned from the function.

Finally, the function is called with the argument 'JavaTpoint' and the result is printed.

Solution - 2

Let's understand the following example.

Example -

Output:

JivoTpaant

Explanation -

In the above code -

  1. We Initialize an empty string called vowels to store the vowels encountered in str1.
  2. Iterate over each character (char) in str1.
  3. Check if char is a vowel by using the in operator to check if it exists in the string "aeiouAEIOU".
  4. If char is a vowel, concatenate it to the vowels string using the += operator.
  5. Initialize an empty string called result_string to store the final result.
  6. Iterate over each character (char) in str1 again.
  7. Check if char is a vowel.
  8. If char is a vowel, concatenate the last character from the vowels string to the result_string using +=.
  9. Remove the last character from the vowels string by using slicing (vowels = vowels[:-1]).
  10. If char is not a vowel, concatenate it directly to the result_string.
  11. Repeat steps 6-10 for each character in str1.
  12. Return the result_string.





Latest Courses