131. Palindrome Partitioning
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Backtracking
- Time
- O(N * 2^N)
- Space
- O(N^2)
Standard Solution (C++)
SummaryGiven a string, find every way to split it into contiguous substrings such that each piece is a palindrome, returning all valid groupings.
Approach
- Precompute a 2-D boolean table
isPalin[i][j]using DP (build from shorter spans to longer) so palindrome checks during backtracking are O(1). - Run DFS/backtracking starting at index 0: at each position, try every endpoint
endin[start, n-1]; ifisPalin[start][end]is true, add that substring to the current partition and recurse fromend+1. - When
start == n, the current partition is complete — push it to the result. - DP precomputation keeps the palindrome check cheap and the overall logic simple and correct.
#include <vector>
#include <string>
#include <functional>
using namespace std;
class Solution {
public:
vector<vector<string>> partition(string s) {
int n = s.size();
// isPalin[i][j] = true iff s[i..j] is a palindrome
vector<vector<bool>> isPalin(n, vector<bool>(n, false));
// Every single character is a palindrome
for (int i = 0; i < n; ++i) isPalin[i][i] = true;
// Fill spans of length 2 and above
for (int len = 2; len <= n; ++len) {
for (int i = 0; i <= n - len; ++i) {
int j = i + len - 1;
if (s[i] == s[j]) {
// Inner span (i+1..j-1) is either empty (len==2) or already palindrome
isPalin[i][j] = (len == 2) || isPalin[i + 1][j - 1];
}
}
}
vector<vector<string>> result;
vector<string> current;
// Backtracking: try every palindromic prefix starting at 'start'
function<void(int)> backtrack = [&](int start) {
if (start == n) {
result.push_back(current);
return;
}
for (int end = start; end < n; ++end) {
if (isPalin[start][end]) {
current.push_back(s.substr(start, end - start + 1));
backtrack(end + 1);
current.pop_back();
}
}
};
backtrack(0);
return result;
}
};
Time O(n * 2^n) — up to 2^n partitions, each requiring O(n) to copy substrings; DP precomputation is O(n^2) Space O(n^2) for the palindrome table plus O(n) recursion depth
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
class Solution {
public:
vector<vector<bool>> pal;
void dfs(vector<vector<string>>& res, vector<string>& cur, string&s, int start){
if(start==s.length()){
res.push_back(cur);
return;
}
for(int end=start; end<s.length(); ++end){
if(pal[start][end]){
cur.push_back(s.substr(start, end-start+1));
dfs(res, cur, s, end+1);
cur.pop_back();
}
}
}
void expansion(const string& s, int i, int j){
if(i>=0 && j<s.length() && s[i]==s[j]){ // i <= j
pal[i][j]=true;
expansion(s, i-1, j+1);
}
}
vector<vector<string>> partition(string s) {
// preprocess: create a lookup table to check palindrome
int len=s.length();
pal.resize(len, vector<bool>(len, false)); // pal[i][j]: s[i...j] is pal or not
// use center expansion method
for(int i=0; i<len; ++i){
expansion(s, i, i);
expansion(s, i, i+1);
}
// dfs to get the result
vector<vector<string>> res;
vector<string> cur;
dfs(res, cur, s, 0);
return res;
}
};
// Time Complexity: O(N * 2^N). 2^N for generating all partitions, O(N) for copying substring to current partition
// Space Complexity: O(N^2) for palindrome lookup table + O(N) for current partition recursion stack Time O(N * 2^N) Space O(N^2)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.