2785 Sort Vowels in a String

Published:

Problem

Solution

Intuition

This problem calls for sorting the vowels in a string by their ASCII value and replacing the vowels in sorted order with consontant placements unchanged.

Approach

This problem is fairly easily solved with the following algorithm:

  1. Store all vowels within the original string
  2. Sort the vowels by their ASCII value
  3. Iterate through the original string and replace any vowel occurence with the next unused sorted vowel.

Complexity

  • Time complexity: $O(nlog(n))$ time complexity as we loop through the string twice and perform constant time operations in each iteration of the loop; however, we sort all vowels which takes $O(nlog(n))$ using Python’s sort

  • Space complexity: $O(n)$ as we store all vowels for sorting and also create a new string to return as the result.

Code

class Solution:
    def sortVowels(self, s: str) -> str:
        vowels = set(['a','e','i','o','u','A','E','I','O','U'])
        t = []
        listvowels = []

        for c in s:
            if c in vowels:
                listvowels.append(c)

        count = 0
        listvowels = sorted(listvowels)

        for i in range(len(s)):
            if s[i] in vowels:
                t.append(listvowels[count])
                count += 1
            else:
                t.append(s[i])

        return "".join(t)