Guest User

Untitled

a guest
Feb 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. /*$
  2. reverseVowels(word) -
  3. Given an array of characters,
  4. reverse the vowels but keep the consonants in place.
  5.  
  6. Space complexity: O(1)
  7. Time complexity: O(N)
  8.  
  9. Example: ["w","h","i","t","e","b","o","a","r","d"] ->
  10. ["w","h","a","t","o","b","e","i","r","d"]
  11.  
  12. */
  13.  
  14. const letters = ["w","h","i","t","e","b","o","a","r","d"];
  15.  
  16. function reverseVowels(word) {
  17. let vowels = /[aeiou]/
  18. let start = 0;
  19. let end = word.length - 1;
  20. let temp;
  21.  
  22.  
  23. while (start !== end) {
  24. if (!word[start].match(vowels) && !word[end].match(vowels)) {
  25. start += 1;
  26. end -= 1;
  27. }
  28.  
  29. if (word[start].match(vowels) && !word[end].match(vowels)) {
  30. start += 1;
  31. }
  32.  
  33. if (!word[start].match(vowels) && word[end].match(vowels)) {
  34. start += 1;
  35. }
  36.  
  37.  
  38. if (word[start].match(vowels) && word[end].match(vowels)) {
  39. temp = word[start];
  40. word[start] = word[end];
  41. word[end] = temp;
  42. start += 1;
  43. end -= 1;
  44. }
  45. }
  46.  
  47. return word;
  48. }
  49.  
  50. console.log(reverseVowels(letters));
Add Comment
Please, Sign In to add comment