Two Pointers · Easy

Valid Palindrome

O(N) · O(1)

Initialize Two Pointers

Array is sorted. Set left pointer L=0 (val 1) and right pointer R=4 (val 1).

Array Elements & Pointers
L
1
L
2
[1]
3
[2]
2
[3]
R
1
R
Live Variables & Invariants
target:4
left:0
right:4
sum:2
Step 1 / 7
14%
Solution Code
1
function isPalindrome(s: string): boolean {
2
  let l = 0, r = s.length - 1;
3
  while (l < r) {
4
    if (s[l] !== s[r]) return false;
5
    l++; r--;
6
  }
7
  return true;
8
}
LeetCode IDE Console

Test Cases

4 cases from Blind 75 & NeetCode 150

Ask for a hint whenever you get stuck.