Example 1
- Input
- s = "aab"
- Output
- [["a","a","b"],["aa","b"]]
The valid cuts give ["a","a","b"] and ["aa","b"]. Keeping "ab" or "aab" as a piece fails because their ends differ. These exhaust the four cut patterns.
Given a string s, return every way to cut it so each piece is a palindrome that reads the same forwards and backwards. Single characters always qualify, so an answer always exists.
Given a string s, cut it into pieces so that every piece reads the same forwards and backwards.
Return all possible ways to cut the string that achieve this, where each way is a list of its palindromic pieces.
A single character always counts as a palindrome, so at least one partitioning always exists.
1 <= s.length <= 16s consists of lowercase English letters.The valid cuts give ["a","a","b"] and ["aa","b"]. Keeping "ab" or "aab" as a piece fails because their ends differ. These exhaust the four cut patterns.
The lone character is a palindrome by itself.
Both all-single-character pieces and the uncut string are valid. The cuts giving ["a","ba"] and ["ab","a"] fail. A longer prefix can be palindromic even when a shorter prefix is not.
Each answer chooses cuts between characters, with no gaps or overlaps. For "aab", choosing "a" leaves "ab", which must split again; choosing "aa" leaves "b". Backtracking means append one valid piece, explore the remaining suffix, then remove the piece before trying another end. A palindrome table can answer whether a proposed piece is valid before the search enters that branch.
Reveal Cut Enumeration: intuition, complexity, and code
Partition searches pair well with precomputation: answer every repeated substring question once in a table, then let the walk spend its budget only on viable prefixes.