Last Updated: November 15, 2025
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Input: s = "a"
Output: [["a"]]
1 <= s.length <= 16s contains only lowercase English letters.The core idea is to use backtracking to generate all possible partitioning of the string, and for each partitioning, check if the substrings are palindromes. Backtracking is well-suited for this type of problem because we can recursively try each possible partition and backtrack if it does not lead to a solution.
i, check if the substring from the start to i is a palindrome.i+1.We can optimize the palindrome checking part by using dynamic programming (DP). Using a 2D DP array, we can store the palindrome status of substrings to avoid recomputing this information repeatedly.
dp where dp[i][j] is true if the substring s[i:j+1] is a palindrome.