Reverse Vowels of a String
Problem
Given a string s, return a new string where only the vowels are reversed in order, while all other characters remain in their original positions.
- 1 ≤ s.length ≤ 3 * 10⁵
- s consists of printable ASCII characters.
Example
s = "hello""holle"The vowels in "hello" are 'e' and 'o'. Reversing their order results in 'o' followed by 'e'. The algorithm uses two pointers starting at the beginning and end of the string, moving inward to find vowels to swap. Initially, left points to 'h' (not a vowel), so it moves right. Right points to 'o' (vowel). When both pointers point to vowels ('e' and 'o'), they are swapped. This process continues until the pointers meet or cross, resulting in the reversed vowel string.
Approach
Straightforward Solution
A naive approach collects all vowels in a list, reverses it, and then reconstructs the string by replacing vowels in order. This requires extra space proportional to the number of vowels and two passes over the string.
Core Observation
The problem requires reversing only vowels in a string, which suggests a two-pointer approach scanning from both ends to efficiently identify vowels to swap without extra space for vowel positions.
Path to Optimal
PreviewThe key insight is to use two pointers starting at the beginning and end of the string, moving inward. Each pointer skips non-vowels until it finds a vowel…
Full step-by-step walkthrough on Pro →
Optimal Approach
PreviewConvert the string to a list for mutability. Use two pointers, left and right, initialized at the start and end of the list…
Full step-by-step walkthrough on Pro →
Want the full reasoning chain?
Unlock the complete walkthrough, line-by-line analysis, and recall drill.
Unlock ProTime
O(n)
Each character is visited at most once by either the left or right pointer, resulting in a single linear pass through the string.
Space
O(n)
The input string is converted to a list of characters to allow in-place swaps, requiring O(n) auxiliary space proportional to the input size. No additional data structures proportional to input size are used.
Pattern Spotlight
Two Pointers (Greedy Contraction)
When reversing or rearranging elements under constraints, use two pointers from opposite ends to identify and swap target elements efficiently, discarding non-target elements by moving pointers inward until the condition is met.
Solution
| 1 | class Solution: |
| 2 | def reverseVowels(self, s: str) -> str: |
| 3 | vowels = set("aeiouAEIOU") |
| 4 | chars = list(s) |
| 5 | |
| 6 | left = 0 |
| 7 | right = len(chars) - 1 |
| 8 | |
| 9 | while left < right: |
| 10 | while left < right and chars[left] not in vowels: |
| 11 | left += 1 |
| 12 | |
| 13 | while left < right and chars[right] not in vowels: |
| 14 | right -= 1 |
| 15 | |
| 16 | chars[left], chars[right] = chars[right], chars[left] |
| 17 | left += 1 |
| 18 | right -= 1 |
| 19 | |
| 20 | return "".join(chars) |
Step-by-Step Solution
Initialize Vowel Set and Convert String to Mutable List
| 3 | vowels = set("aeiouAEIOU") |
| 4 | chars = list(s) |
Objective
To prepare for efficient vowel detection and enable in-place character swaps.
Key Insight
Using a set for vowels allows O(1) membership checks, which is critical for performance when scanning large strings. Converting the immutable string to a list enables swapping characters directly without creating new strings repeatedly, which would be costly.
Interview Quick-Check
Core Logic
A set of vowels provides constant-time membership checks, which is essential for efficient filtering during pointer traversal.
Common Pitfalls & Bugs
Forgetting to convert the string to a list would make swapping impossible or inefficient, as strings are immutable in Python.
Use Two Pointers to Locate and Swap Vowels While Moving Inward
To identify vowels from both ends and swap them to reverse their order in the string.
Reconstruct and Return the Resulting String
To convert the modified list of characters back into a string for the final output.
2 more steps with full analysis available on Pro.
Line Analysis
This solution has 1 Critical line interviewers watch for.
chars[left], chars[right] = chars[right], chars[left]
Swap the vowels at the left and right pointers.
Swapping vowels reverses their order in the string while preserving the positions of non-vowel characters, which is the core operation of the algorithm.
Full line-by-line criticality + rationale for all 13 lines available on Pro.
Test Your Understanding
Why does moving the pointers inward only when vowels are found guarantee that all vowels are reversed correctly?
See the answer with Pro.
Related Problems
Two Pointers pattern
Don't just read it. Drill it.
Reconstruct Reverse Vowels of a String from memory until it sticks. AlgoDrill blanks out key lines and makes you fill them back in, step by step.
Unlock the Reverse Vowels of a String drill