Intuition
- Place the vowels that you saw into a list, sort them, and then replace every vowel with the new occurrence.
Complexity
Runtime
, where N is the length of the string, and V is the amount of vowels which are sorted thereafter.
Space
to create a list of the original string
Code
class Solution:
def sortVowels(self, s: str) -> str:
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
s_list = list(s)
vowels_in_s = []
for i, letter in enumerate(s_list):
if letter in vowels:
vowels_in_s.append(letter)
vowels_in_s.sort(reverse=True)
for i, letter in enumerate(s_list):
if letter in vowels:
s_list[i] = vowels_in_s.pop()
return ''.join(s_list)