Reverse Vowels of a String
Problem statement
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 cases.
Example 1:
Input: s = "hello"Output: "holle"
Example 2:
Input: s = "leetcode"Output: "leotcede"
Constraints:
1 <= s.length <= 3 * 105sconsist of printable ASCII characters.
My solution
/**
* @param {string} s
* @return {string}
*/
var reverseVowels = function(s) {
const vowelIndex = []
const vowels = new Set(["a", "e", "i", "o", "u"]);
for (let i = 0; i < s.length; i++) {
const curr = s.charAt(i);
if (vowels.has(curr) || vowels.has(curr.toLowerCase())) {
vowelIndex.push(curr)
}
}
let str= "";
for (let i = 0; i < s.length; i++) {
const curr = s.charAt(i);
if (vowels.has(curr) || vowels.has(curr.toLowerCase())) {
str += vowelIndex.pop();
} else {
str += curr;
}
}
return str
};