#2785
Sort Vowels in a String
pupil · 410 · lc medium +25 · verified · 83.4% accepted · 1,426 likes · top 95%
Description
Given a 0-indexed string s, rearrange it into a new string t obeying these rules:
- Consonants stay in their original positions: for any index i where s[i] is a consonant, t[i] = s[i].
- Vowels are sorted by non-decreasing ASCII value: for indices i < j where both s[i] and s[j] are vowels, t[i]'s ASCII value must not exceed t[j]'s.
Return the resulting string.
The vowels are 'a', 'e', 'i', 'o', and 'u' in both cases. Everything else is a consonant.
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".
Code
1
2
3