Skip to content

Instantly share code, notes, and snippets.

@clayton-perszyk
Created February 10, 2018 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clayton-perszyk/e2735cc7219f08bf9b2ffa3614e33873 to your computer and use it in GitHub Desktop.
Save clayton-perszyk/e2735cc7219f08bf9b2ffa3614e33873 to your computer and use it in GitHub Desktop.
/*$
reverseVowels(word) -
Given an array of characters,
reverse the vowels but keep the consonants in place.
Space complexity: O(1)
Time complexity: O(N)
Example: ["w","h","i","t","e","b","o","a","r","d"] ->
["w","h","a","t","o","b","e","i","r","d"]
*/
const letters = ["w","h","i","t","e","b","o","a","r","d"];
function reverseVowels(word) {
let vowels = /[aeiou]/
let start = 0;
let end = word.length - 1;
let temp;
while (start !== end) {
console.log(start + " " + end)
if (!word[start].match(vowels)) {
start += 1;
continue;
}
if (!word[end].match(vowels)) {
end -= 1;
continue;
}
temp = word[start];
word[start] = word[end];
word[end] = temp;
start += 1;
end -= 1;
}
console.log(start + " " + end)
return word;
}
console.log(reverseVowels(letters));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment