-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2785-sort-vowels-in-a-string.py
More file actions
35 lines (29 loc) · 979 Bytes
/
2785-sort-vowels-in-a-string.py
File metadata and controls
35 lines (29 loc) · 979 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
# time complexity: O(nlogn)
# space complexity: O(n)
from collections import defaultdict
class Solution:
def sortVowels(self, s: str) -> str:
vowels = set(["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"])
vowelMap = defaultdict(int)
for char in s:
if char in vowels:
vowelMap[char] += 1
if not vowelMap:
return s
sortedVowels = sorted(vowelMap.keys(), key=lambda x: ord(x))
vowelIdx = 0
result = ""
for char in s:
if char not in vowels:
result += char
else:
vowelMap[sortedVowels[vowelIdx]] -= 1
result += sortedVowels[vowelIdx]
if vowelMap[sortedVowels[vowelIdx]] == 0:
del vowelMap[sortedVowels[vowelIdx]]
vowelIdx += 1
return result
s = "lEetcOde"
print(Solution().sortVowels(s))
s = "lYmpH"
print(Solution().sortVowels(s))