-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 146 ms (61.99%), Space: 19.1 MB (60.99%) - LeetHub
- Loading branch information
Showing
1 changed file
with
15 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
2887-sort-vowels-in-a-string/2887-sort-vowels-in-a-string.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |