Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created February 4, 2017 21:01
Show Gist options
  • Save primaryobjects/5af99a12fc047f9d10c26b2faf6a374b to your computer and use it in GitHub Desktop.
Save primaryobjects/5af99a12fc047f9d10c26b2faf6a374b to your computer and use it in GitHub Desktop.
Reverse Vowels of a String
/* Approach 1. Loop through string and store vowels on array, placeholders in string. Then loop through string again and replace in vowels in reverse order. */
var reverseVowels = function(s) {
var result = '';
var string = [];
var vowels = [];
// Start from front of string to find consonants and vowels.
for (var i=0; i<s.length; i++) {
var orig = s[i];
var char = s[i].toLowerCase();
if (char === 'a' || char === 'e' || char === 'i' || char === 'o' || char === 'u') {
vowels.push(orig);
orig = null; // placeholder
}
string.push(orig);
}
// Insert vowels.
for (var i=0; i<string.length; i++) {
if (!string[i]) {
string[i] = vowels.splice(vowels.length - 1, 1)[0];
}
}
result = string.join('');
return result;
}
/* Approach 2. Loop through string and convert to array, storing vowels in 2nd array. Then loop through vowels and swap in two at a time in reverse order. Finally, join array back into string. */
/**
* @param {string} s
* @return {string}
*/
var reverseVowels = function(s) {
var result = '';
var string = [];
var vowels = { A: 1, a: 1, E: 1, e: 1, I: 1, i: 1, O: 1, o: 1, U: 1, u: 1 };
var vowelsToReverse = [];
for (var i=0; i<s.length; i++) {
if (vowels[s[i]]) {
// Store the character and its position (to swap with end of list).
vowelsToReverse.push({ char: s[i], pos: i });
}
// Regardless, just append.
string.push(s[i]);
}
// Insert vowels into position.
while (vowelsToReverse.length) {
var vowel1 = vowelsToReverse.splice(0, 1)[0];
var vowel2 = null;
if (vowelsToReverse.length) {
vowel2 = vowelsToReverse.splice(vowelsToReverse.length - 1, 1)[0];
}
if (vowel2) {
string[vowel1.pos] = vowel2.char;
string[vowel2.pos] = vowel1.char
}
}
result = string.join('');
return result;
};
/* Approach 3. Fastest! Convert the string to an array. Maintain a pointer at the front and end of string.
End will point to the first vowel found from the end.
If the current letter is a consonant, do nothing and advance to next letter.
If the current letter is a vowel, swap it with the end pointer, and decrement end to the next vowel.
When the front passes the end, we're done.
*/
var reverseVowels = function(s) {
var result = '';
var str = s.split('')
var end = s.length - 1;
for (var i=0; i<str.length; i++) {
if (i > end) {
// We're done.
break;
}
// Find first vowel from end of string.
while (end >= 0) {
if ('aAeEiIoOuU'.indexOf(str[end]) !== -1) {
// We have a vowel.
break;
}
end--;
}
if ('aAeEiIoOuU'.indexOf(str[i]) === -1) {
// Consonant, do nothing.
}
else {
// Vowel, swap with end.
var temp = str[end];
str[end] = str[i];
str[i] = temp;
end--;
}
}
result = str.join('');
return result;
}
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Note:
The vowels does not include the letter "y".
@Subbu23M
Copy link

code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment