Reorder only the vowels in a string while keeping all consonants in their original positions.
Problem
Given a string s, sort all vowels in s in nondecreasing order while leaving every non-vowel character exactly where it is.
Vowels are a, e, i, o, u, and their uppercase variants.
Return the modified string after the vowels have been rearranged.
Goal
Only the vowels may move. All other characters must stay in the same indices they originally occupied.
Input Format
- A single string
s. smay contain lowercase letters, uppercase letters, and possibly other characters depending on the test setting.
Treat vowels case-sensitively for ordering, but identify both lowercase and uppercase vowels as vowels.
Output Format
- Return a string of the same length as
s. - The vowels in the returned string must appear in sorted order from left to right.
- Non-vowel characters must remain unchanged in their original positions.
Constraints
1 <= s.length <= $10^{5}$- The relative order of non-vowel characters must not change.
- Only the vowel characters are rearranged.
- Use an approach that is efficient enough for linear or near-linear processing of the string.
Example 1
Input
s = "lEetcOde"
Output
"lEOtcede"
Explanation
The vowels are E, e, O, e. Sorted by character order, they become E, O, e, e. Putting them back into the vowel slots gives lEOtcede.
Example 2
Input
s = "leetcode"
Output
"leetcedo"
Explanation
The vowels are e, e, o. Sorted order is still e, e, o, so only their positions are rearranged accordingly.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.