-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path345.py
More file actions
41 lines (32 loc) · 843 Bytes
/
345.py
File metadata and controls
41 lines (32 loc) · 843 Bytes
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
35
36
37
38
39
40
41
'''
345. Reverse Vowels of a String
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".
'''
class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
s_str = [c for c in s]
head = 0
tail = len(s)-1
vowels ='aeiouAEOUI'
while head < tail:
if s_str[head] not in vowels:
head+=1
elif s_str[tail] not in vowels:
tail-=1
else:
s_str[head], s_str[tail] = s_str[tail], s_str[head]
tail-=1
head+=1
return ''.join(s_str)