-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseVowels.py
More file actions
76 lines (70 loc) · 1.89 KB
/
reverseVowels.py
File metadata and controls
76 lines (70 loc) · 1.89 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#Source : https://leetcode.com/problems/reverse-vowels-of-a-string/
#Author : Yuan Wang
#Date : 2018-06-24
'''
**********************************************************************************
*Write a function that takes a string as input and reverse only the vowels of a string.
*
*Example 1:
*Given s = "hello", return "holle".
*
*Example 2:
*Given s = "leetcode", return "leotcede".
*
*Note:
*The vowels does not include the letter "y".
**********************************************************************************/
'''
#Self solution one,generate a list of all the vowels and reverse them in the list
#order of them and rebuild a string with reversed order of vowels
#Time complexity:O(n), Space complexity:O(n)
def reverseVowelsA(s):
"""
:type s: str
:rtype: str
"""
vowels=[string for string in s if string.lower() in"aeiou"]
lst=set(vowels)
result=""
index=len(vowels)-1
for i in s:
if i not in lst:
result+=i
else:
result+=vowels[index]
index-=1
return result
#self solution two, generate a list of indexes of all vowels
#using two pointer swap each pair of vowels until the middle of the original string
#Time complexity:O(n), Space complexity:O(n)
def reverseVowelsB(s):
"""
:type s: str
:rtype: str
"""
vowel=[i for i,char in enumerate(s) if char.lower() in "aeiou"]
string=list(s)
for i in range(len(vowel)//2):
string[vowel[i]],string[vowel[len(vowel)-i-1]]=string[vowel[len(vowel)-1-i]],string[vowel[i]]
return "".join(string)
#Other solution using two pointer
#Time complexity:O(n), Space complexity:O(n)
def reverseVowels(s):
"""
:type s: str
:rtype: str
"""
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
L = list(s)
i = 0
j = len(L) - 1
while i < j:
while i < j and L[i] not in vowels:
i += 1
while j > i and L[j] not in vowels:
j -= 1
L[i], L[j] = L[j], L[i]
i += 1
j -= 1
return ''.join(L)
print(reverseVowels("hello"))