const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Patterns

LeetCode Palindrome Problems — Complete Pattern Guide

Palindrome problems span 5 difficulty tiers and 4 techniques — here is how to recognize which approach to use in under 30 seconds.

11 min read|

LeetCode Palindrome Problems: 4 Techniques, 5 Difficulty Levels

From two pointers to DP to backtracking — know which approach to use in 30 seconds

Why Palindrome Problems Cover the Full Spectrum of leetcode palindrome Difficulty

Palindrome problems are one of the few problem families on LeetCode that span every difficulty level — Easy, Easy-Medium, Medium, Hard, and Hard-competitive — while requiring completely different algorithmic techniques at each tier. A candidate who has only seen Easy palindrome problems and encounters a Hard palindrome partitioning question in an interview will reach for the wrong tool and fail not because they lack knowledge, but because they did not recognize the category shift.

The four technique families are: two pointers for validation, expand-around-center for substrings, dynamic programming for subsequences, and backtracking for partitioning. Each is appropriate for a specific class of problem. Palindrome problems on LeetCode divide cleanly into 4 technique families — two pointers for validation, expand-around-center for substrings, DP for subsequences, and backtracking for partitioning — choosing wrong costs you the entire solution.

This guide maps every major LeetCode palindrome problem to its correct technique, explains why that technique is the right choice, and gives you a decision framework you can apply in under 30 seconds during an interview. By the end, you will not just know how to solve each problem — you will know immediately which approach to reach for when a new palindrome problem lands in front of you.

What Is a Palindrome? — Formal Definition and Types

A palindrome is any sequence that reads the same forwards and backwards. The formal definition: a string s is a palindrome if s[i] == s[n-1-i] for all valid indices i, where n is the length of the string. The simplest palindromes are single characters (trivially palindromic) and two equal characters like "aa". Longer examples include "racecar", "madam", and "abacaba".

LeetCode palindrome problems come in four structural types. String palindromes ask whether a character sequence satisfies the definition — these map to two pointers or expand-around-center. Number palindromes (LeetCode #9) ask whether an integer reads the same forwards and backwards without converting to a string. Linked list palindromes (#234) ask the same question over a pointer-linked structure. Subsequence palindromes ask for the longest sub-sequence (not necessarily contiguous) that forms a palindrome — these require dynamic programming.

Interviews love palindromes for several reasons: they test index manipulation, they have multiple valid approaches at different complexity levels, they scale from trivial to highly non-trivial with small problem variations, and the "almost palindrome" and "partition into palindromes" variants require genuinely advanced techniques. Recognizing the structural type of the palindrome problem — not just the word "palindrome" in the title — is the first skill this guide builds.

  • String palindrome — reads same forwards and backwards; use two pointers or expand-around-center
  • Number palindrome — integer whose digit sequence reverses to itself; use modular arithmetic
  • Linked list palindrome — pointer structure that mirrors; use fast/slow pointer + reversal
  • Palindromic substring — contiguous substring that is a palindrome; use expand-around-center
  • Palindromic subsequence — non-contiguous subsequence that is a palindrome; use DP
  • Palindrome partition — split a string so every piece is a palindrome; use backtracking + DP

Technique 1 — Two Pointers for leetcode palindrome Validation (Easy Tier)

Two pointers is the correct technique when you need to validate whether an existing string or number is a palindrome. Place one pointer at index 0 and one at index n-1, compare the characters, advance the left pointer forward and the right pointer backward, and stop if any pair fails to match. This runs in O(n) time and O(1) space and is optimal for validation tasks.

LeetCode #9 — Palindrome Number — extends this to integers without converting to a string. The key insight: reverse only the second half of the number and compare it to the first half. This avoids integer overflow that would result from reversing the entire number. Numbers ending in 0 (except 0 itself) and negative numbers are immediately not palindromes.

LeetCode #125 — Valid Palindrome — adds the real-world constraint of ignoring non-alphanumeric characters and case differences. The two-pointer approach still works perfectly: advance each pointer past non-alphanumeric characters before each comparison, and compare using case-insensitive equality. The core algorithm is unchanged; only the "advance" step adds filtering logic.

LeetCode #680 — Valid Palindrome II — introduces the "one deletion allowed" twist. The insight: use two pointers as normal. When you hit a mismatch, you have exactly two choices — delete the left character or delete the right character. Check if either resulting substring (after the skipped character) is a palindrome using the same two-pointer helper. If either is, the answer is true. This is O(n) time and O(1) space and demonstrates how a small problem variation changes the solution structure without changing the core technique.

  • #9 Palindrome Number — reverse second half, compare to first half; O(log n) time, O(1) space
  • #125 Valid Palindrome — two pointers with alphanumeric filter and case folding; O(n)
  • #680 Valid Palindrome II — two pointers, on mismatch try skipping either character; O(n)

Technique 2 — Expand Around Center for Palindromic Substrings (Medium Tier)

When the problem asks you to find or count palindromic substrings within a larger string, expand-around-center is the canonical O(n^2) technique. The idea: for every position in the string, treat it as the center of a potential palindrome and expand outward as long as the characters match. A palindrome can be centered on a single character (odd-length) or on the gap between two characters (even-length), so every position generates two expansion attempts.

The "expand around center" technique for Longest Palindromic Substring (#5) runs in O(n^2) time and O(1) space — while Manacher's Algorithm achieves O(n), interviewers rarely expect it and "expand around center" is the expected answer. This is important to know: do not spend time implementing Manacher's unless explicitly asked. The O(n^2) solution is the industry-standard interview answer and is sufficient for all practical interview cases.

LeetCode #5 — Longest Palindromic Substring — asks for the longest contiguous substring that is a palindrome. Run expand-around-center at every position, tracking the longest palindrome found. Time: O(n^2). Space: O(1) beyond the result string. The naive O(n^3) approach of checking all substrings is too slow for n up to 1000.

LeetCode #647 — Palindromic Substrings — asks for the count of all palindromic substrings (not just the longest). Use the same expand-around-center loop but increment a counter each time the expansion succeeds instead of tracking the maximum length. Each successful expansion step adds exactly one new palindrome. Both problems share the same core loop — the only difference is whether you track max length or a running count.

💡

Always Check Both Odd-Length and Even-Length Centers

For each index i, run two expansions: one centered at i (odd-length palindromes, e.g., "aba"), and one centered between i and i+1 (even-length palindromes, e.g., "abba"). A common bug is checking only odd-length centers and missing even-length palindromes entirely. Both expansions use the same helper — just pass (i, i) for odd and (i, i+1) for even.

Technique 3 — Dynamic Programming for Palindromic Subsequences (Hard Tier)

When the problem involves subsequences (not contiguous substrings) or asks you to count rather than find palindromes across exponentially many possibilities, dynamic programming is required. The key distinction: a subsequence can skip characters; a substring cannot. This single word difference changes the entire algorithm family.

LeetCode #516 — Longest Palindromic Subsequence — asks for the length of the longest subsequence of a string that forms a palindrome. The DP recurrence is: dp[i][j] represents the longest palindromic subsequence in s[i..j]. If s[i] == s[j], dp[i][j] = dp[i+1][j-1] + 2. Otherwise, dp[i][j] = max(dp[i+1][j], dp[i][j-1]). This is O(n^2) time and O(n^2) space. The equivalent problem (find length of longest common subsequence of s and reverse(s)) gives the same answer and can serve as a useful cross-check.

Count Palindromic Subsequences problems (counting all distinct palindromic subsequences) are among the harder DP variants. They require tracking both count and structure using interval DP with careful handling of repeated characters. These rarely appear in standard interviews but are common in competitive programming rounds and occasionally in onsite loops at companies with the hardest algorithm bars.

The DP approach applies broadly to any palindrome problem where you are operating on characters that are not necessarily contiguous and need to aggregate over all valid configurations. The signal for "use DP": the problem says "subsequence" (not "substring"), or the problem asks you to count over all valid palindromic decompositions.

  • #516 Longest Palindromic Subsequence — interval DP; dp[i][j] = dp[i+1][j-1]+2 if s[i]==s[j], else max(dp[i+1][j], dp[i][j-1])
  • Equivalent to LCS of s and reverse(s) — useful as a mental cross-check
  • Fill DP table from shorter intervals to longer (bottom-up by interval length)
  • Space optimization: can reduce to O(n) space using two rolling rows
  • Count Palindromic Subsequences — harder variant; requires tracking character frequencies inside intervals

Technique 4 — Backtracking for Palindrome Partitioning

Palindrome Partitioning problems ask you to split a string into parts such that every part is a palindrome. These problems require backtracking because the answer space is a collection of all valid partition configurations — you cannot find it with a single linear scan or a simple DP table without also enumerating the partitions.

LeetCode #131 — Palindrome Partitioning — asks for all possible ways to partition a string such that every substring in the partition is a palindrome. The approach: backtracking over all prefix lengths at each position, recursing only when the current prefix is a palindrome. To avoid recomputing palindrome checks on every backtrack step, precompute a 2D boolean DP table isPalin[i][j] in O(n^2) before starting the backtracking. This brings the palindrome-check inside the recursion down from O(n) to O(1).

LeetCode #132 — Palindrome Partitioning II — asks only for the minimum number of cuts needed so every piece is a palindrome (not all configurations). This drops the backtracking and becomes a pure 1D DP problem: dp[i] = minimum cuts for s[0..i], with dp[i] = min(dp[j-1] + 1) for all j where s[j..i] is a palindrome. Combined with the same isPalin precomputation, this runs in O(n^2). The key insight: Partitioning II is fundamentally a different problem class than Partitioning I — one is enumeration (backtracking), the other is optimization (DP).

The precomputed isPalin table is the bridge between backtracking and DP in both problems. Compute it once with an expand-around-center approach (technique 2) to fill isPalin[i][j] = true when the substring s[i..j] is a palindrome. Then both #131 and #132 reference this table in O(1) per lookup, making the overall algorithms efficient despite operating over exponential partition spaces.

  • #131 Palindrome Partitioning — backtracking + precomputed isPalin table; enumerate all valid partitions
  • #132 Palindrome Partitioning II — 1D DP with isPalin lookup; find minimum cut count
  • Precompute isPalin[i][j] once using expand-around-center for O(1) palindrome checks during search
  • #131 returns List<List<String>>; #132 returns a single integer — completely different output shapes
  • For #131, the recursion branches on every index where isPalin[start][i] is true

Choosing the Right leetcode palindrome Technique — Decision Flowchart

The decision between the four techniques maps cleanly to four questions you can answer in under 30 seconds. First: does the problem ask you to validate whether something is a palindrome, or to find/count/partition palindromes? Validation always means two pointers. If the problem goes beyond validation, continue.

Second: does the problem involve substrings (contiguous) or subsequences (non-contiguous)? If the problem uses the word "subsequence" or asks about characters that can be skipped, use DP. If it uses the word "substring" or involves contiguous character sequences, use expand-around-center. Third: if the problem involves substrings, does it ask you to find or count palindromic substrings within a larger string? Both map to expand-around-center with a trivial output difference (max vs counter). Fourth: does the problem ask you to partition a string into palindromes? If finding all partitions, use backtracking with isPalin precomputation. If finding the minimum cuts, use 1D DP with the same isPalin table.

One additional signal: if the problem gives you a single string and asks for "all ways" to do something palindrome-related, it is almost always backtracking. If it asks for "minimum", "maximum", or "count" of a palindrome quantity over all possible configurations, it is almost always DP.

ℹ️

4-Technique Decision Flowchart

Step 1: "Validate a palindrome?" → Two Pointers (#9, #125, #680). Step 2: "Subsequence (can skip chars)?" → DP (#516). Step 3: "Find/count palindromic substrings?" → Expand Around Center (#5, #647). Step 4: "Partition into palindromes?" → if all partitions: Backtracking + isPalin (#131); if minimum cuts: 1D DP + isPalin (#132). The isPalin precomputation table bridges techniques 3 and 4 — always build it in O(n^2) before solving either partitioning problem.

Conclusion — Drill the Decision, Not Just the Solutions

Palindrome problems reward pattern recognition more than any other LeetCode problem family. The algorithms themselves — two pointers, expand-around-center, interval DP, backtracking — are not uniquely palindrome techniques. What is unique is knowing immediately which one to reach for when you see a palindrome problem, and doing so before writing a single line of code.

The most common mistake candidates make is defaulting to two pointers for every palindrome problem because it is the first technique they learned. Two pointers answers exactly one question: is this string/number a palindrome? For everything else — finding substrings, counting occurrences, working with subsequences, partitioning — a different technique is required, and applying two pointers will produce either a wrong answer or an exponentially slow solution.

The practice path: solve #125 and #9 first to internalize two pointers, then #5 and #647 back-to-back to build the expand-around-center muscle memory (paying attention to the odd/even center distinction), then #516 to learn interval DP on palindromes, then #131 and #132 together to see how the same isPalin precomputation serves both backtracking and DP. YeetCode flashcards help you drill the decision framework — not just the code — so the technique selection becomes automatic under interview pressure.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now