Leetcode•Nov 08, 2026
Reverse Vowels of a String
Hazrat Ali
Leetcode
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
Example 1:
Input: s = "IceCreAm"
Output: "AceCreIm"
Explanation:
The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm".
Example 2:
Input: s = "leetcode"
Output: "leotcede"
Solution
var reverseVowels = function(s) {
const chars = s.split('');
const vowels = "aeiouAEIOU";
let left = 0;
let right = chars.length - 1;
while (left < right) {
while (left < right && !vowels.includes(chars[left])) {
left++;
}
while (left < right && !vowels.includes(chars[right])) {
right--;
}
[chars[left], chars[right]] = [chars[right], chars[left]];
left++;
right--;
}
return chars.join('');
};