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
Reverse Vowels of a StringTransform: "hello" → "holle"Step 1: Input StringhelloVowels: e, o (green boxes)Step 2: Reverse VowelsSWAPStep 3: Resultholle"holle"
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
Asked in
Microsoft 35 Amazon 25 Facebook 20
85.0K Views
Medium Frequency
~15 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen