Community Problem
Reverse Vowels of a String
Difficulty: Easy
Reverse only the vowels of a string while keeping consonants and punctuation in place.
Reverse Vowels of a String
Reverse only the vowels of a string while keeping consonants and punctuation in place.
By @meinakamura
January 12, 2026
·
Updated May 20, 2026
788 views
5
4.3 (13)
Saw this on a screening loop two weeks ago, and the candidate spent eight minutes building a frequency map before realizing the problem was just Reverse String with a filter. The trap is that the wording ("reverse only the vowels") nudges you toward sorting or counting, when the real shape is two pointers that skip consonants. Worth keeping in your warm-up rotation right after Reverse String.
Reverse Vowels of a String
Given a string s, reverse only the vowels of the string and return it. The vowels are 'a', 'e', 'i', 'o', 'u', in both lowercase and uppercase. All other characters keep their original positions; the relative order of vowels themselves is reversed.
Examples
Example 1:
- Input:
s = "hello" - Output:
"holle" - Explanation: The vowels are
'e'and'o'at indices 1 and 4. Swapping them produces"holle".
Example 2:
- Input:
s = "leetcode" - Output:
"leotcede" - Explanation: Vowels are
e, e, o, eat indices 1, 2, 6, 7. Reversed sequence ise, o, e, e, which yields"leotcede".
Example 3:
- Input:
s = "AEIOU" - Output:
"UOIEA" - Explanation: Every character is a vowel, so the result equals the full reverse.
Example 4:
- Input:
s = "sky" - Output:
"sky" - Explanation: No vowels in the string, so the output equals the input.
Constraints
1 <= s.length <= 3 * 10^5sconsists of printable ASCII characters.
Follow-up
How would the algorithm change if the vowel set were configurable (for instance, treating 'y' as a vowel in some dialects)?
Solution
Hints
Approach
Two pointers, but each pointer skips past non-vowels before swapping. The vowel set is small and fixed (10 characters across both cases), so membership testing is O(1). The structure is identical to Reverse String, with two added inner skip-loops.
Algorithm
Reverse-vowels
1. left = 0, right = len(s) - 1
2. while left < right:
advance left past non-vowels
retreat right past non-vowels
if left < right: swap, then step both inward
3. return the rebuilt stringComplexity
| Metric | Cost |
|---|---|
| Time | O(n) |
| Space | O(n) |
Time is linear because each index is visited at most twice (once by left, once by right). Space is O(n) for the mutable character array; both JavaScript and Python require this because their string types are immutable.
Why it works
Vowels and non-vowels never trade places: a non-vowel never moves, and a vowel only moves to a position previously held by another vowel. So at the end of the walk, the multiset of vowels at vowel-positions has been reversed, and every consonant / punctuation slot is untouched. That is exactly the spec.
Pitfalls
- Forgetting uppercase vowels. The spec includes
A, E, I, O, U. A vowel-set that only has lowercase characters silently fails on inputs like"Aa". - Mutating the input string in JS / Python. Both are immutable. Build a list / array, swap there, then join at the end.
- Using
for...ofover a Map of vowels in JS Sandpack. This problem does not need a Map, but if you reach for one, remember Sandpack's ES5 transpile breaks Map iteration. A plain string check ('aeiouAEIOU'.indexOf(ch) !== -1) sidesteps the trap entirely.
Solution Code
