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 = "IceCreAm"
Output: "AceCreIm"
Explanation:
The vowels in s
are ['I', 'e', 'e', 'A']
. On reversing the vowels, s becomes "AceCreIm"
.
Example 2:
Input: s = "leetcode"
Output: "leotcede"
Constraints:
1 <= s.length <= 3 * 105
s
consist of printable ASCII characters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for reversing vowels in a string looks at each character one by one. Whenever it finds a vowel, it stores the vowel. Then, it goes through the string again and replaces the vowels in reverse order from how they appeared originally.
Here's how the algorithm would work step-by-step:
def reverse_vowels_of_a_string_brute_force(input_string):
vowels_found = []
# Collect all vowels from the input string
for char in input_string:
if char in 'aeiouAEIOU':
vowels_found.append(char)
string_as_list = list(input_string)
vowel_index = len(vowels_found) - 1
# Replace vowels in the string with vowels from the end of our list
for index in range(len(string_as_list)):
if string_as_list[index] in 'aeiouAEIOU':
# Replace vowel with one from list in reverse order
string_as_list[index] = vowels_found[vowel_index]
vowel_index -= 1
return "".join(string_as_list)
To efficiently reverse only the vowels in a string, we can use a two-pointer approach. We'll have one pointer at the beginning and one at the end, moving towards each other, swapping vowels as we find them.
Here's how the algorithm would work step-by-step:
def reverse_vowels(input_string):
vowels = "aeiouAEIOU"
string_list = list(input_string)
start_index = 0
end_index = len(input_string) - 1
while start_index < end_index:
# Find the next vowel from the start.
while start_index < end_index and string_list[start_index] not in vowels:
start_index += 1
# Find the next vowel from the end.
while end_index > start_index and string_list[end_index] not in vowels:
end_index -= 1
# Ensure pointers haven't crossed.
if start_index < end_index:
# Swap the vowels.
string_list[start_index], string_list[end_index] = string_list[end_index], string_list[start_index]
# Move to the next positions.
start_index += 1
end_index -= 1
return "".join(string_list)
Case | How to Handle |
---|---|
Null or empty string input | Return null or an empty string respectively to handle invalid input. |
String with no vowels | Return the original string as no vowels need to be reversed. |
String with only one vowel | Return the original string as there's nothing to reverse. |
String with only vowels | Reverse the entire string as it only contains vowels. |
String with mixed case vowels | Treat uppercase and lowercase vowels equally during identification and reversal. |
String with non-ASCII characters | Ensure the vowel check considers Unicode vowels beyond basic ASCII. |
Very long string (potential performance bottleneck) | Use a two-pointer approach to optimize for O(n) time complexity. |
String containing only consonants | The algorithm should return the original string, as no vowels exist to reverse. |