Valid Palindrome | LeetCode 125

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 {boolean}
 */
let isPalindrome = function(s) {
  let pointerOne = 0;
  let pointerTwo = s.length - 1;

  while (pointerOne < pointerTwo) {
    while (!isValidCharacter(s.charAt(pointerOne))) {
      pointerOne++;
    }

    while(!isValidCharacter(s.charAt(pointerTwo))) {
      pointerTwo--;
    }

    if (s.charAt(pointerOne).toLowerCase() !== s.charAt(pointerTwo).toLowerCase()) {
      return false;
        
    }

    pointerOne++;
    pointerTwo--;
  }

  return true;
};

let isValidCharacter = function(char) {
  let alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
    
  return alphabet.indexOf(char.toLowerCase()) > -1;
};