Skip to content

Commit

Permalink
Time: 146 ms (61.99%), Space: 19.1 MB (60.99%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Nov 13, 2023
1 parent 586d6ae commit 79a9072
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 2887-sort-vowels-in-a-string/2887-sort-vowels-in-a-string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def sortVowels(self, s: str) -> str:
# Step 1: Collect vowels and sort them in descending order
vowels_sorted = sorted([c for c in s if c.lower() in 'aeiou'], reverse=True)

# Step 2: Construct the answer string by replacing vowels in sorted order
result = []
for char in s:
if char.lower() in 'aeiou':
result.append(vowels_sorted.pop())
else:
result.append(char)

# Step 3: Join the characters to form the final string
return ''.join(result)

0 comments on commit 79a9072

Please sign in to comment.