diff --git a/2887-sort-vowels-in-a-string/2887-sort-vowels-in-a-string.py b/2887-sort-vowels-in-a-string/2887-sort-vowels-in-a-string.py new file mode 100644 index 0000000..2a3b678 --- /dev/null +++ b/2887-sort-vowels-in-a-string/2887-sort-vowels-in-a-string.py @@ -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) \ No newline at end of file