Every Way to Split a String Into Palindromic Pieces
Implement allPalindromicPartitions
Given a string
s, find every way to cut it into consecutive pieces so that each individual piece reads the same forwards and backwards. A single character always qualifies on its own, so at minimum the all-single-character split always works — the question is which other, coarser splits also happen to work.
Trying every possible cut pattern and validating the resulting partition afterward finds every answer, but it builds and discards a lot of doomed partitions along the way — one non-palindromic piece anywhere invalidates the whole thing, yet that's only discovered once every piece has already been carved out. Checking each piece the moment it's proposed, before it's ever added to the path, means a doomed choice costs only the check itself — nothing gets built around it first.
Example 1:
Input: s = "abba"
Output: [["a","b","b","a"],["a","bb","a"],["abba"]]
Example 2:
Input: s = "xyz"
Output: [["x","y","z"]]
Example 3:
Input: s = "q"
Output: [["q"]]
+ 5 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ s.length ≤ 8 - ●
s consists only of lowercase English letters - ●
Results are returned sorted (each partition compared element-by-element, shorter first on a tie) for a stable, checkable answer
s =
abba