Sort Vowels in a String - Problem
Given a string s, you need to create a new string where all consonants stay in their original positions but all vowels are sorted by their ASCII values.
Think of it like organizing a library: the shelves (consonant positions) stay fixed, but you rearrange the books (vowels) in alphabetical order within those shelves.
The Rules:
- ๐ Consonants must remain in their exact original positions
- ๐ Vowels (a, e, i, o, u - both lowercase and uppercase) should be sorted by ASCII value
- ๐ฏ ASCII sorting means: A < E < I < O < U < a < e < i < o < u
Example: "lEetcOde" โ "lEOtcede"
Vowels found: [E, e, O, e] โ Sorted: [E, O, e, e]
Place them back: lE*tcOde โ lE*tcede
Input & Output
example_1.py โ Basic Case
$
Input:
s = "lEetcOde"
โบ
Output:
"lEOtcede"
๐ก Note:
The vowels are [E, e, O, e]. After sorting by ASCII: [E, O, e, e]. Placing them back: l(E)*(O)tc(e)d(e) = "lEOtcede". Consonants l, t, c, d stay in their original positions.
example_2.py โ Mixed Case
$
Input:
s = "lYmpH"
โบ
Output:
"lYmpH"
๐ก Note:
No vowels found in the string (Y is not considered a vowel in this problem). The string remains unchanged since only consonants are present.
example_3.py โ All Vowels
$
Input:
s = "AeIoU"
โบ
Output:
"AeIoU"
๐ก Note:
All characters are vowels: [A, e, I, o, U]. After ASCII sorting: [A, I, U, e, o]. Result: "AIUeo". Wait, that's wrong... Let me recalculate: ASCII values: A(65), I(73), U(85), e(101), o(111). So sorted: A, I, U, e, o โ "AIUeo"
Constraints
- 1 โค s.length โค 105
- s consists only of letters (both uppercase and lowercase)
- Vowels are: a, e, i, o, u, A, E, I, O, U
- All other letters are considered consonants
Visualization
Tap to expand
Understanding the Visualization
1
Identify Positions
Mark vowel positions (red) and consonant positions (blue) in the original string
2
Extract & Sort
Remove all vowels and sort them by ASCII values: A-Z comes before a-z
3
Replace in Order
Put sorted vowels back into vowel positions sequentially
4
Final Result
Consonants unchanged, vowels properly sorted by ASCII values
Key Takeaway
๐ฏ Key Insight: The two-pass approach is optimal - first collect and sort all vowels, then place them back sequentially in vowel positions while preserving consonant positions.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code