Reverse Vowels of a String
Easy
Topics
Given a string s, reverse only the vowels (a e i o u, both cases) and return the result.
Example 1
Input: s = "hello" Output: "holle"
Example 2
Input: s = "leetcode" Output: "leotcede"
Constraints
- 1 <= s.length <= 3*10^5
Editorial — intuition, approach, complexity and pitfalls
Intuition
Only the vowels move, and they swap with each other from the outside in — consonants stay exactly where they are. So this is the two-pointer reversal, with a filter: advance each pointer until it lands on a vowel, then swap the two vowels and step both inward.
The structure is worth noticing because it generalises: "reverse only the elements satisfying P, leaving the rest in place" is always two pointers that skip non-matching positions.
Approach
- A set of the ten vowels, both cases, for O(1) membership.
leftandrightat the ends. Ifleftis not a vowel, advance it; else ifrightis not a vowel, retreat it; else swap and move both.
The three-branch if/elif/else matters: advance only one pointer per iteration until both are on vowels, then swap.
Dry Run
s = "leetcode" -> ['l','e','e','t','c','o','d','e']
left=0 'l' not vowel -> left=1
left=1 'e', right=7 'e' both vowels -> swap (no visible change)
left=2 right=6
left=2 'e', right=6 'd' not vowel -> right=5
left=2 'e', right=5 'o' both vowels -> swap
['l','o','e','t','c','e','d','e'] left=3 right=4
left=3 't' not vowel -> left=4
left=4 == right=4, loop ends
answer "leotcede"Complexity
- Time — O(n): each pointer only ever moves inward, so together they traverse the string once.
- Space — O(n) in Python, because strings are immutable and the characters must be copied into a list to be swapped. In a language with mutable strings or on a char array this is O(1). The vowel set is O(1), being ten fixed entries.
Common Pitfalls
- Forgetting uppercase vowels. The set needs both cases, or lowercase the character before testing.
- Advancing both pointers in the same iteration while skipping, which can step over a vowel.
- Collecting the vowels, reversing them, and writing them back — correct, and O(n) extra, where two pointers need none beyond the mutable buffer.
- Treating
'y'as a vowel. It is not, for this problem.
Full Solution — all languages
class Solution:
def reverseVowels(self, s):
v = set("aeiouAEIOU")
a = list(s)
left, right = 0, len(a) - 1
while left < right:
if a[left] not in v:
left += 1
elif a[right] not in v:
right -= 1
else:
a[left], a[right] = a[right], a[left]
left += 1
right -= 1
return "".join(a)import java.util.*;
class Solution {
public String reverseVowels(String s) {
String v = "aeiouAEIOU";
char[] a = s.toCharArray();
int left = 0, right = a.length - 1;
while (left < right) {
if (v.indexOf(a[left]) < 0) left++;
else if (v.indexOf(a[right]) < 0) right--;
else {
char t = a[left];
a[left] = a[right];
a[right] = t;
left++;
right--;
}
}
return new String(a);
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: string reverseVowels(string s) {
string v = "aeiouAEIOU";
int left = 0, right = (int)s.size() - 1;
while (left < right) {
if (v.find(s[left]) == string::npos) left++;
else if (v.find(s[right]) == string::npos) right--;
else {
swap(s[left], s[right]);
left++;
right--;
}
}
return s;
}
};/**
* @param {string} s
* @return {string}
*/ var reverseVowels = function(s) {
const v = "aeiouAEIOU";
const a = s.split('');
let left = 0, right = a.length - 1;
while (left < right) {
if (v.indexOf(a[left]) < 0) left++;
else if (v.indexOf(a[right]) < 0) right--;
else {
const t = a[left];
a[left] = a[right];
a[right] = t;
left++;
right--;
}
}
return a.join('');
};