Back to Roadmap
Week 1Day 3 of 35

Two Pointers

Compare both ends at the same time instead of rescanning everything.

Day Progress0%

0 of 3 problems solved

Pattern Focus

Two Pointers

Compare both ends at the same time instead of rescanning everything.

Pattern Checklist
  • Does the answer depend on both ends of the string?
  • Should I skip spaces or punctuation first?
  • Can two pointers meet in the middle?
🔁New to DSA? Start here 🧠

A palindrome is just something that reads the same from both ends, like looking at a word in a mirror. So the problem becomes much simpler if you use two pointers: one starts on the left, one on the right, and both move toward the middle. If the characters keep matching, you keep going. If they ever disagree, the answer is immediately false. No fancy trick, just a clean way to compare mirror positions.

When both ends matter, check both ends at the same time.

How to think about it
  1. 1Place one pointer at the start of the string and one at the end.
  2. 2If you are ignoring spaces or punctuation, skip those characters before comparing.
  3. 3Compare the left and right characters.
  4. 4If they match, move inward. If they do not match, return false right away.
  5. 5If the pointers cross, the string is a palindrome.
🚧Common Mistake

Forgetting to skip spaces or punctuation leads to false mismatches even when the cleaned string is a palindrome.

🔍Problem Hints

Valid Palindrome II

Same two-pointer approach as Valid Palindrome. When characters mismatch, try skipping left or right — one resulting substring must be a palindrome.

Palindrome Pairs

For every word, a reverse-lookup HashMap maps each word's reverse to its index. Check prefix/suffix palindrome conditions to find valid pairs.

Problems

Valid Palindrome

easy

Valid Palindrome II

medium

Palindrome Pairs

hard