AlgoMaster Logo

Palindrome String Check

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

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.

Key Constraints:

  • 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.
  • Lowercase alphanumeric only → Characters can be compared directly with ==. There is no normalization step for case or symbols.

Approach 1: Reverse and Compare

Intuition

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.

Algorithm

  1. Build a new string that holds the characters of s in reverse order.
  2. Compare the reversed string with the original s.
  3. Return true if they are equal, and false otherwise.

Example Walkthrough

1Original string is "abca".
0
a
1
b
2
c
3
a
1/3

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.

false
result

Code

Approach 2: Two Pointers

Intuition

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.

Algorithm

  1. Set left = 0 and right = s.length - 1.
  2. While left < right, compare s[left] and s[right].
  3. If they differ, return false.
  4. Otherwise increment left and decrement right.
  5. If the loop finishes without a mismatch, return true.

Example Walkthrough

1left = 0, right = 6. Compare 'r' and 'r'. They match.
0
left
r
1
a
2
c
3
e
4
c
5
a
6
r
right
1/4

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.

true
result

Code