Created
February 10, 2018 00:13
-
-
Save clayton-perszyk/e2735cc7219f08bf9b2ffa3614e33873 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*$ | |
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