Palindrome Check
Receives a string of text and checks whether it reads the same forwards and backwards. It uses the classic two-pointer technique: one pointer starts at the beginning, the other at the end, and on each step they compare the characters they point to. As soon as a pair does not match, the string cannot be a palindrome and the check stops immediately; otherwise both pointers move one position toward each other until they meet (an odd-length string leaves its middle character unchecked) or cross (an even-length string has every pair compared). The comparison is case-sensitive and exact — no letters, spaces or punctuation are ignored. Returns true when every pair of characters matches, and false otherwise.
Visualization
- Input
- Result
Algorithm code
// Palindrome Check — a single pure function. Checks whether a string reads
// the same forwards and backwards using the classic two-pointer scan: one
// pointer starts at the beginning, the other at the end, and on each step
// they compare the characters they point to. As soon as a pair does not
// match the string cannot be a palindrome and the function returns early;
// otherwise the pointers move one step toward each other until they meet or
// cross. No side effects.
/**
* @param {string} s - the string to check, e.g. "racecar"
* @returns {boolean} true if s reads the same forwards and backwards
*/
export function isPalindrome(s) {
const chars = [...s];
let left = 0;
let right = chars.length - 1;
while (left < right) {
if (chars[left] !== chars[right]) {
return false;
}
left += 1;
right -= 1;
}
return true;
} FUNCTION isPalindrome(s):
chars ← characters of s
left ← 0
right ← length(chars) - 1
WHILE left < right:
IF chars[left] != chars[right]:
RETURN false
left ← left + 1
right ← right - 1
RETURN true