↓Skip to main content
  1. LeetCode/

LeetCode 3913: Sort Vowels by Frequency

·2 mins· ·
LeetCode Medium String Sorting Counting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-04-26

  • Submissions: Accepted (first try)

Approach
#

Consonants stay put; only vowels are rearranged (String Template 4):

  1. Scan once, recording each vowel’s count and first position.
  2. Extract the vowels in order and sort by (-count, first position): higher counts first, ties by earlier first appearance.
  3. Scan again and fill each vowel position with the next sorted vowel.

My Solution
#

class Solution:
    def sortVowels(self, s: str) -> str:
        vowels = set("aeiou")
        count, first = Counter(), {}
        for i, ch in enumerate(s):
            if ch in vowels:
                count[ch] += 1
                first.setdefault(ch, i)
        ordered = sorted((ch for ch in s if ch in vowels), key=lambda ch: (-count[ch], first[ch]))
        it = iter(ordered)
        return "".join(next(it) if ch in vowels else ch for ch in s)

Complexity
#

Time O(n log n), space O(n). With only 5 vowels, expanding each vowel’s count in order gives O(n).

Mistakes to Avoid
#

  • Ties are broken by first occurrence, not alphabetical order.
  • Only vowel positions change; consonants stay where they are.

Interview Explanation
#

I count each vowel and record its first position, extract the vowels and sort them by (negative count, first position), then put them back into the vowel positions in order. Time O(n log n), or O(n) since there are only 5 vowels.

Related

LeetCode 522: Longest Uncommon Subsequence II
·2 mins
LeetCode Medium Two-Pointers String Sorting Array Hash-Table
LeetCode 522 note: the longest uncommon subsequence is always a whole string; check strings from longest to shortest, and the first that is not a subsequence of any other long-enough string is the answer
LeetCode 3805: Count Caesar Cipher Pairs
·2 mins
LeetCode Medium Math Hash-Table String Counting
LeetCode 3805 note: shift each string so its first letter is a, making Caesar-equivalent strings identical, then count pairs per normalized form
LeetCode 3839: Number of Prefix Connected Groups
·1 min
LeetCode Medium Hash-Table String Counting
LeetCode 3839 note: words sharing the first k characters are connected, so count by word[:k]; exclude words shorter than k
LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 3639: Minimum Time to Activate String
·3 mins
LeetCode Medium Binary-Search Counting String
LeetCode 3639 note: each new star adds (left distance) × (right distance) new substrings; or binary search on time
LeetCode 3545: Minimum Deletions for At Most K Distinct Characters
·1 min
LeetCode Easy Greedy Hash-Table String Sorting Counting
LeetCode 3545 note: reducing the number of kinds means deleting whole kinds, starting from the least frequent