Last Updated: June 7, 2026
A palindrome reads identically left to right and right to left. "racecar" qualifies because the first and last characters match, the second and second-to-last match, and so on toward the center. "hello" does not, because the first character h does not match the last character o.
The input is clean: already lowercase, only letters and digits, so there is no need to skip spaces or strip punctuation. Every character participates exactly as it appears.
Two boundary cases need a clear answer. An empty string has no characters to compare, so it returns true. A single character mirrors itself, so it is a palindrome too. Both fall out of a check that returns true when no mismatched pair is found.
0 <= s.length <= 1000 → The string can be empty, which is a palindrome. The code must not assume there is at least one character to inspect.==. There is no normalization step for case or symbols.A palindrome that reads the same backward as forward equals its own reverse. So build the reversed version of s and check whether it equals s. No index bookkeeping is needed, just the reverse and one equality comparison.
The cost is space. Building the reversed string allocates another n characters, which is O(n) extra space.
s in reverse order.s.true if they are equal, and false otherwise.Take the string "abca". Reversing it gives "acba". Now compare the two character by character. The first characters both equal a, but the second character of the original is b while the second character of the reverse is c. That mismatch is enough to conclude the string is not a palindrome, so the answer is false. For a string like "abba", the reverse is also "abba", the comparison succeeds everywhere, and the answer is true.
Building a full reversed copy does more work than needed. The mirrored characters can be compared directly on the original string, with no copy.
Place one pointer at the start and another at the end. If the characters differ, return false. If they match, move the left pointer right and the right pointer left, then compare again. Continue until the pointers meet or cross, which means every mirrored pair agreed.
This uses O(1) extra space, just the two indices, and it stops early on the first mismatch.
left = 0 and right = s.length - 1.left < right, compare s[left] and s[right].false.left and decrement right.true.Take the string "racecar". Start with left = 0 and right = 6, pointing at r and r. They match, so move inward to left = 1 and right = 5, pointing at a and a. They match, so move inward to left = 2 and right = 4, pointing at c and c. They match again, so move inward to left = 3 and right = 3. The pointers now meet at the center character e, the condition left < right is false, and the loop stops. No mismatch was found, so the string is a palindrome and the answer is true.