-
Notifications
You must be signed in to change notification settings - Fork 0
/
345.py
34 lines (27 loc) · 855 Bytes
/
345.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'''
Descripttion: 反转字符串中的元音字母
version: 1
Author: Jason
Date: 2020-11-23 10:16:45
LastEditors: Jason
LastEditTime: 2020-11-23 10:23:33
'''
class Solution:
def reverseVowels(self, s: str) -> str:
words = list(s)
left = 0
right = len(words) - 1
vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}
while left < right:
while left < right and words[left] not in vowels:
left += 1
while left < right and words[right] not in vowels:
right -= 1
if left < right:
words[left], words[right] = words[right], words[left]
left += 1
right -= 1
return "".join(words)
s = Solution()
string = "leetcode"
print(s.reverseVowels(string))