Reverse Vowels of a String — Solution in Python, Java & C++
Reverse Vowels of a String solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Reverse Vowels of a String is an easy coding problem on strings and two pointers, asked in Amazon, Google and Adobe interviews.
- Difficulty: Easy
- Topics: Strings, Two Pointers
- Asked at: Amazon, Google, Adobe
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given a string s, reverse only the vowels in it and return the result. Every other character stays exactly where it is.
The vowels are a, e, i, o and u, in either case.
Example 1
Input: s = "hello"
Output: holle
Explanation: The vowels e and o swap places.
Example 2
Input: s = "leetcode"
Output: leotcede
Example 3
Input: s = "sky"
Output: sky
Explanation: No vowels, so nothing moves.
Constraints
1 <= s.length <= 40s consists of English letters.
Hints for Reverse Vowels of a String
Hint 1
Two pointers walking inward, each skipping over consonants until it lands on a vowel.
Hint 2
Swap the two vowels, then step both pointers past them.
Hint 3
Stop as soon as the pointers cross.
How to solve Reverse Vowels of a String
Two pointers from each end skip over non-vowels, swap the vowels they land on, and step inward.
Approach
- Copy the string into a character array; set
left = 0,right = n - 1. - Advance
leftuntil it points at a vowel; retreatrightuntil it points at a vowel. - If
left < right, swap and move both inward; repeat until they cross.
Why it works
The sequence of vowel positions is fixed; pairing the k-th vowel from the left with the k-th from the right and swapping reverses the vowel sequence while every other character stays put.
Complexity
- Time —
O(n) - Space —
O(n) for the copy
Pitfalls
- Include both cases in the vowel set —
Aandaboth count. - Both inner loops need the
left < rightguard, or they can run past each other.
Reference solution
Python
def reverseVowels(s: str) -> str:
vowels = set("aeiouAEIOU")
chars = list(s)
left, right = 0, len(chars) - 1
while left < right:
while left < right and chars[left] not in vowels:
left += 1
while left < right and chars[right] not in vowels:
right -= 1
if left < right:
chars[left], chars[right] = chars[right], chars[left]
left += 1
right -= 1
return "".join(chars)Java
public static String reverseVowels(String s) {
String vowels = "aeiouAEIOU";
char[] chars = s.toCharArray();
int left = 0, right = chars.length - 1;
while (left < right) {
while (left < right && vowels.indexOf(chars[left]) < 0) left++;
while (left < right && vowels.indexOf(chars[right]) < 0) right--;
if (left < right) {
char t = chars[left];
chars[left] = chars[right];
chars[right] = t;
left++;
right--;
}
}
return new String(chars);
}C++
string reverseVowels(string s) {
string vowels = "aeiouAEIOU";
int left = 0, right = (int) s.size() - 1;
while (left < right) {
while (left < right && vowels.find(s[left]) == string::npos) left++;
while (left < right && vowels.find(s[right]) == string::npos) right--;
if (left < right) {
char t = s[left];
s[left] = s[right];
s[right] = t;
left++;
right--;
}
}
return s;
}JavaScript
var reverseVowels = function(s) {
const isVowel = function(c) { return "aeiouAEIOU".indexOf(c) >= 0; };
const chars = s.split("");
let left = 0, right = chars.length - 1;
while (left < right) {
while (left < right && !isVowel(chars[left])) left++;
while (left < right && !isVowel(chars[right])) right--;
if (left < right) {
const t = chars[left];
chars[left] = chars[right];
chars[right] = t;
left++;
right--;
}
}
return chars.join("");
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings · Two Pointers Technique