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.
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.
Input: s = "hello"
Input: s = "leetcode"
We can solve this problem using the various approaches.
Solution - 1:
Let's see the following solution -
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:
- The input string s is converted into a list of characters using the list() function, allowing us to modify individual characters later on.
- 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.
- The code enters a while loop that continues as long as i is less than j.
- Inside the loop, there are several conditional statements to handle different cases:
- 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]).
- 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.
- 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.
- 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.
- 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.
- 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.
In the above code -
- We Initialize an empty string called vowels to store the vowels encountered in str1.
- Iterate over each character (char) in str1.
- Check if char is a vowel by using the in operator to check if it exists in the string "aeiouAEIOU".
- If char is a vowel, concatenate it to the vowels string using the += operator.
- Initialize an empty string called result_string to store the final result.
- Iterate over each character (char) in str1 again.
- Check if char is a vowel.
- If char is a vowel, concatenate the last character from the vowels string to the result_string using +=.
- Remove the last character from the vowels string by using slicing (vowels = vowels[:-1]).
- If char is not a vowel, concatenate it directly to the result_string.
- Repeat steps 6-10 for each character in str1.
- Return the result_string.