Palindrome Partitioning
Difficulty: Medium
A palindrome reads the same forwards and backwards, like "aba" or "racecar" (a single letter always is one). Given a string, find every way to split it into consecutive pieces where every piece is a palindrome.
Examples
Input: s = "aab"
Output: [["a","a","b"], ["aa","b"]]
Splitting into "a","a","b" works because each piece reads the same both ways. Splitting into "aa","b" also works. Splitting into "a","ab" does not, because "ab" isn't a palindrome.
Input: s = "a"
Output: [["a"]]
A single letter is always a palindrome by itself, and there's only one piece possible.
Constraints
1 <= s.length <= 16
s consists only of lowercase English letters.
Approach
Build a partition one piece at a time. From the current position, try every length for the next piece. If that substring is a palindrome, take it and recurse on the rest. Reaching the end of the string means every piece was a palindrome, so the path is a valid partition.
The simple version re-checks each substring from scratch by comparing characters from both ends inward. That works, but the same substring is often checked many times.
A faster version precomputes "is s[i..j] a palindrome" for every i and j. s[i..j] is a palindrome exactly when s[i] equals s[j] and s[i+1..j-1] is also a palindrome. With that table, each check during the search is an instant lookup.
Solutions
Backtracking — Check Each Cut Directly
From a given position, try every end point for the next piece. Check each piece from both ends inward; if it's a palindrome, add it, recurse from right after it, then remove it before trying the next end point.
function partition(s) {
const result = [];
const current = [];
function isPalindrome(start, end) {
while (start < end) {
if (s[start] !== s[end]) return false;
start++;
end--;
}
return true;
}
function backtrack(start) {
if (start === s.length) {
result.push([...current]);
return;
}
for (let end = start; end < s.length; end++) {
if (isPalindrome(start, end)) {
current.push(s.slice(start, end + 1));
backtrack(end + 1);
current.pop();
}
}
}
backtrack(0);
return result;
}Time: O(n * 2^n) · Space: O(n) recursion depth, plus output storage
Optimal — Precompute Palindromes with a DP Table
Before searching, build a table isPalindrome[i][j] for whether s[i..j] is a palindrome, filling shorter substrings first. s[i..j] is a palindrome exactly when s[i] === s[j] and the substring inside is also a palindrome (or too short to need checking). During the search, each check is a table lookup instead of an O(n) scan.
function partition(s) {
const n = s.length;
const isPalindrome = Array.from({ length: n }, () => new Array(n).fill(false));
for (let end = 0; end < n; end++) {
for (let start = end; start >= 0; start--) {
if (s[start] === s[end] && (end - start <= 2 || isPalindrome[start + 1][end - 1])) {
isPalindrome[start][end] = true;
}
}
}
const result = [];
const current = [];
function backtrack(start) {
if (start === n) {
result.push([...current]);
return;
}
for (let end = start; end < n; end++) {
if (isPalindrome[start][end]) {
current.push(s.slice(start, end + 1));
backtrack(end + 1);
current.pop();
}
}
}
backtrack(0);
return result;
}Time: O(n^2) to precompute the table, plus O(n * 2^n) to generate all partitions - the partition count is unavoidable, but each palindrome check is now O(1) instead of O(n) · Space: O(n^2) for the DP table, plus O(n) recursion depth