345
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入:"hello"
输出:"holle"
示例 2:
输入:"leetcode"
输出:"leotcede"
解法
- Python
class Solution:
def reverseVowels(self, s: str) -> str:
vowel = ['a','e','i','o','u','A','E','I','O','U']
strls = [i for i in s]
left,right = 0,len(strls)-1
while left < right:
while (strls[right] not in vowel) and left < right:
right -= 1
while (strls[left] not in vowel) and left < right:
left += 1
strls[left],strls[right] = strls[right], strls[left]
left += 1
right -= 1
return ''.join(strls)