Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1: Given s = "hello", return "holle".
Example 2: Given s = "leetcode", return "leotcede".
Note: The vowels does not include the letter "y".
Two Pointers
class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
s = list(s)
start = 0
end = len(s)-1
vowels = 'aeiouAEIOU'
while start < end:
# bug: while s[start] not in vowels:
while s[start] not in vowels and start < end:
start += 1
# bug: while s[end] not in vowels:
while s[end] not in vowels and start < end:
end -= 1
s[start], s[end] = s[end], s[start]
start += 1
end -= 1
return ''.join(s)
Last updated
Was this helpful?