Problem

Qn - Sort Vowels in a String

Approach

Pretty simple, we just remember the indexes where the vowels are located, extract them out and sort them, then insert them back.

Complexity

  • Time:
  • Space: for the array, we can do without it too though

Code

class Solution:
    def sortVowels(self, s: str) -> str:
        vowel_index = []
        vowels = []
        all_vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}
        s = list(s)
 
        for i,c in enumerate(s):
            if c in all_vowels:
                vowel_index.append(i)
                vowels.append(c)
            
        vowels.sort()
 
        for i,ind in enumerate(vowel_index): s[ind] = vowels[i]
 
        return "".join(s)