-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_vowels_of_string.py
39 lines (31 loc) · 1.03 KB
/
reverse_vowels_of_string.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
35
36
37
38
39
class Solution:
"""
Task:
Given a string s, reverse only all the vowels in the string and return it.
ls are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
"""
# Method 1
def reverseVowels(self, s: str) -> str:
s = list(s)
vowels = 'aeiouAEIOU'
l, r = 0, len(s) - 1
while (l < r):
while (l < r and s[l] not in vowels):
l += 1
while (r > l and s[r] not in vowels):
r -= 1
s [l], s[r] = s[r], s[l]
l +=1
r -=1
return "".join(s)
# Method 2
def reverseVowels(self, s: str) -> str:
vowels = {'a','A','e','E','i','I','o','O','u','U'}
s = list(s)
### extract all vowels from s
vowelInS = [c for c in s if c in vowels]
### replace the vowels in s with the reversed ones
for i in range(len(s)):
if s[i] in vowels:
s[i] = vowelInS.pop(-1)
return ''.join(s)