Valid palindrome (two pointers)

Easy TimeO(n) SpaceO(1)

A palindrome reads the same forwards and backwards, like "racecar". Given a string s, return true when it is one, comparing character by character from both ends inwards rather than by recursion. A single mismatch settles it, and an empty string or a single character is a palindrome.

Examples

Example 1

Input
s = 'racecar'
Output
true
r and r, a and a, c and c, with e alone in the middle.

Example 2

Input
s = 'abca'
Output
false
The outer as match, but b and c do not.

The Code

function isPalindrome(s) {
  let i = 0;
  let j = s.length - 1;
  while (i < j) {
    if (s[i] !== s[j]) return false;
    i++;
    j--;
  }
  return true;
}
isPalindrome('racecar');
Done
Step through isPalindrome('racecar') call by call