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.
Example 1:
Input: s = "hello"
Output: "holle"
Example 2:
Input: s = "leetcode"
Output: "leotcede"
JavaScript
function reverseVowels(s: string): string {
let length = s.length;
let splitString = s.split('');
let vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
for(let i = 0, j = length - 1; i < j; ) {
if(!vowels.includes(splitString[i])) {
i++;
}
if(!vowels.includes(splitString[j])) {
j--;
}
if(vowels.includes(splitString[i]) && vowels.includes(splitString[j])) {
let temp = splitString[i];
splitString[i] = splitString[j];
splitString[j] = temp;
i++;
j--;
}
}
return splitString.join('');
};
//top
most of them used similar approach so saving this regex sol
function reverseVowels(s: string): string {
const vow = s.match(/[aeiou]/ig);
return s.replace(/[aeiou]/ig, el=> vow.pop());
};
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.