Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Input: "hello"
Output: "holle"
Example 2:
Input: "leetcode"
Output: "leotcede"
Note:
The vowels does not include the letter "y".
Solution in python:
class Solution:
def reverseVowels(self, s: str) -> str:
vowels = ['a', 'o', 'e', 'i', 'u', 'A', 'O', 'E', 'I', 'U']
s = list(s)
i = 0
j = len(s)-1
while i < j:
bool1 = s[i] in vowels
bool2 = s[j] in vowels
if bool1 and bool2:
s[i], s[j] = s[j], s[i]
i += 1
j -= 1
elif bool1 and (not bool2):
j -= 1
elif (not bool1) and bool2:
i += 1
else:
i += 1
j -= 1
return ''.join(s)
留言