-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path345_Reverse_Vowels_of_a_String.py
More file actions
45 lines (36 loc) · 976 Bytes
/
345_Reverse_Vowels_of_a_String.py
File metadata and controls
45 lines (36 loc) · 976 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
42
43
44
45
"""
345. Reverse Vowels of a String
Easy
558
1002
Add to List
Share
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:
def reverseVowels(self, s: str) -> str:
vow = "aeiou"
string = list(s)
i,j = 0, len(s)-1
while i < j:
if string[i].lower() not in vow:
i+=1
elif string[j].lower() not in vow:
j-=1
else:
string[i],string[j] = string[j], string[i]
i+=1
j-=1
return "".join(string)
"""
Runtime: 56 ms, faster than 56.66% of Python3 online submissions for Reverse Vowels of a String.
Memory Usage: 14.8 MB, less than 6.67% of Python3 online submissions for Reverse Vowels of a String.
"""