1. Ends inward
Two pointers start at the ends and move toward each other.
let left = 0;
let right = s.length - 1Typorithm · easy
Step 1 of 8 · intro card
Two Pointers
Two pointers walk inward from both ends, skipping non-alphanumerics and comparing normalized characters.
A palindrome reads the same forward and backward, so compare characters from both ends inward.
Skip any character that is not a letter or digit before each comparison.
Lowercase both characters and compare; one mismatch means it is not a palindrome.
Pointers converge from the ends.
Skip non-alphanumeric characters.
Compare case-insensitively.
function isPalindrome(s) {
const isAlphaNum = (ch) => /[a-z0-9]/i.test(ch);
let left = 0;
let right = s.length - 1;
while (left < right) {
while (left < right && !isAlphaNum(s[left])) {
left += 1;
}
while (left < right && !isAlphaNum(s[right])) {
right -= 1;
}
if (s[left].toLowerCase() !== s[right].toLowerCase()) {
return false;
}
left += 1;
right -= 1;
}
return true;
}Connect each part of the code to the concept before you type.
Two pointers start at the ends and move toward each other.
let left = 0;
let right = s.length - 1Advance past characters that are not letters or digits.
while (left < right && !isAlphaNum(s[left])) {
left += 1;
}Mismatched normalized characters break the palindrome.
if (s[left].toLowerCase() !== s[right].toLowerCase()) {After a match, move both pointers one step in.
left += 1;
right -= 1Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.