Sort Vowels in a String - Complete Solution Guide
Sort Vowels in a String is LeetCode problem 2785, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a 0-indexed string s , permute s to get a new string t such that: All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i] . The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices i , j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j] . Return the resulting string . T
Detailed Explanation
The problem asks us to sort the vowels within a given string while preserving the positions of all consonants. The vowels should be sorted in non-decreasing order based on their ASCII values. The string contains both uppercase and lowercase English letters. For example, given the string 'lEetcOde', we need to return 'lEOtcede' where 'E', 'O', and 'e' (vowels) are sorted.
Solution Approach
The solution iterates through the input string and counts the occurrences of each vowel (both uppercase and lowercase). It stores these counts in an array `vowel_counts` indexed by the ASCII value of the character. Then, it iterates through a pre-defined order of vowels (`ordered_vowel_chars`), constructing a sorted list of vowels (`sorted_vowels`) based on the counts. Finally, it iterates through the input string again, replacing each vowel with the next vowel from the sorted list, while leaving the consonants in their original positions.
Step-by-Step Algorithm
- Step 1: Initialize a set `vowels_set` to efficiently check if a character is a vowel.
- Step 2: Initialize an array `vowel_counts` of size 128 (to accommodate all ASCII characters) to store the count of each vowel.
- Step 3: Iterate through the input string `s`. If a character is a vowel, increment its corresponding count in `vowel_counts`.
- Step 4: Create a list `sorted_vowels` by iterating through a pre-defined ordered list of vowels `ordered_vowel_chars`. For each vowel, add it to `sorted_vowels` as many times as it appears in `vowel_counts`.
- Step 5: Create an iterator `sorted_vowels_iter` for the `sorted_vowels` list.
- Step 6: Initialize an empty list `result_chars` to store the characters of the result string.
- Step 7: Iterate through the original string `s` again. If a character is a vowel, append the next vowel from `sorted_vowels_iter` to `result_chars`. Otherwise, append the original character to `result_chars`.
- Step 8: Join the characters in `result_chars` to form the final sorted string.
Key Insights
- Insight 1: Identifying vowels is a straightforward but essential part. We can use a set or similar data structure for efficient lookup.
- Insight 2: Counting the frequency of each vowel allows us to create a sorted sequence without needing to perform a full sorting algorithm on the entire string.
- Insight 3: Creating an ordered list of vowels (A, E, I, O, U, a, e, i, o, u) to iterate through simplifies the sorting and guarantees correct ASCII order.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: String, Sorting.