Reverse Vowels of a String | LeetCode 345

AFFILIATE LINKS

Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard


Here is the full implementation.

/**
 * @param {string} s
 * @return {string}
 */
let reverseVowels = function(s) {
    let stringArr = s.split('');
    let pointer1 = 0;
    let pointer2 = stringArr.length - 1;

    while (pointer1 < pointer2) {
      while (pointer1 < pointer2) {
        if (!isVowel(stringArr[pointer1])) {
          pointer1++;
        } else {
          break;
        }
      }

      while (pointer1 < pointer2) {
        if (!isVowel(stringArr[pointer2])) {
          pointer2--;
        } else {
          break;
        }
      }

      let temp = stringArr[pointer1];
      stringArr[pointer1] = stringArr[pointer2];
      stringArr[pointer2] = temp;
      pointer1++;
      pointer2--;
    }

    return stringArr.join('');
};

let isVowel = function(char) {
  let vowels = 'aeiou';

  return vowels.includes(char.toLowerCase());
}