Reverse Vowels of a String - Problem
Given a string s, reverse only all the vowels in the string and return it.
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
Input & Output
Example 1 — Basic Case
$
Input:
s = "hello"
›
Output:
"holle"
💡 Note:
The vowels are 'e' and 'o'. After reversing them, we get 'o' and 'e', resulting in "holle"
Example 2 — Mixed Case Vowels
$
Input:
s = "leetcode"
›
Output:
"leotcede"
💡 Note:
The vowels are 'e', 'e', 'o', 'e'. After reversing: 'e', 'o', 'e', 'e', resulting in "leotcede"
Example 3 — Single Vowel
$
Input:
s = "aA"
›
Output:
"Aa"
💡 Note:
Two vowels 'a' and 'A'. After reversing positions, we get "Aa"
Constraints
- 1 ≤ s.length ≤ 3 × 105
- s consists of printable ASCII characters
Visualization
Tap to expand
Understanding the Visualization
1
Input
Original string with vowels highlighted
2
Process
Identify and reverse only the vowels
3
Output
String with vowels in reversed positions
Key Takeaway
🎯 Key Insight: Two pointers efficiently swap vowels from both ends without needing extra storage for vowel collection
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code