Solving LeetCode Problem 2785: “Sort Vowels in a String” — A Comprehensive Guide
3 minute read
LeetCode’s problem 2785, “Sort Vowels in a String,” poses a unique challenge in string manipulation. The task is to reorder vowels in a given string based on their ASCII values while keeping consonants in their original positions. This article delves into the theory and logic behind solving this problem, using Python as the language of choice.
Question Description
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
with0 <= i < s.length
such thats[i]
is a consonant, thent[i] = s[i]
. - The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices
i
,j
with0 <= i < j < s.length
such thats[i]
ands[j]
are vowels, thent[i]
must not have a higher ASCII value thant[j]
.
Return the resulting string.
The vowels are 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.
Example 1:
Input: s = "lEetcOde"
Output: "lEOtcede"
Explanation: 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
Example 2:
Input: s = "lYmpH"
Output: "lYmpH"
Explanation: There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
Constraints:
1 <= s.length <= 105
s
consists only of letters of the English alphabet in uppercase and lowercase.
Theory and Logic
The solution to this problem involves several key steps:
- Identification of Vowels: First and foremost, we need to recognize which characters are vowels. Vowels in English are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, and they can appear in both lowercase and uppercase.
- Extraction and Sorting of Vowels: Once identified, we extract these vowels from the string. After extraction, these vowels are sorted based on their ASCII values. This sorting ensures that they are in the nondecreasing order required by the problem statement.
- Reconstruction of String: The final step is to reconstruct the string. During this process, we place the sorted vowels back into their original positions while ensuring that the consonants remain unchanged.
Python Implementation
Here is the Python implementation encapsulated in a class, as per typical LeetCode format:
class Solution:
def sortVowels(self, s: str) -> str:
vowels = set("aeiouAEIOU")
vowel_list = [c for c in s if c in vowels] # Extract vowels
vowel_list.sort() # Sort vowels based on ASCII values
result = []
vowel_index = 0 # To track the index of the next vowel in the sorted list
for char in s:
if char in vowels:
result.append(vowel_list[vowel_index]) # Place the next sorted vowel
vowel_index += 1
else:
result.append(char) # Keep consonants in place
return ''.join(result)
Examples and Explanation
Let’s consider some examples to illustrate this solution:
Example 1:
- Input:
s = "lEetcOde"
- Vowels in
s
:E
,e
,O
- Sorted vowels:
E
,O
,e
- Output:
lEOtcede
Here, the vowels are sorted and placed back in their original positions, while consonants like l
, t
, c
, d
remain unchanged.
Example 2:
- Input:
s = "lYmpH"
- Vowels in
s
: None - Output:
lYmpH
In this case, since there are no vowels, the original string is returned as it is.
Conclusion
The “Sort Vowels in a String” problem is an excellent exercise in string manipulation and sorting. By understanding how to segregate different types of characters, sort them, and then reassemble the string, one can gain valuable insights into handling similar problems in Python. This solution demonstrates an efficient and straightforward approach to solving this particular LeetCode challenge.