Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*$
- 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) {
- if (!word[start].match(vowels) && !word[end].match(vowels)) {
- start += 1;
- end -= 1;
- }
- if (word[start].match(vowels) && !word[end].match(vowels)) {
- start += 1;
- }
- if (!word[start].match(vowels) && word[end].match(vowels)) {
- start += 1;
- }
- if (word[start].match(vowels) && word[end].match(vowels)) {
- temp = word[start];
- word[start] = word[end];
- word[end] = temp;
- start += 1;
- end -= 1;
- }
- }
- return word;
- }
- console.log(reverseVowels(letters));
Add Comment
Please, Sign In to add comment