Skip to content
BytePatterns

Split Into Palindromes

MediumBacktracking#backtracking#pruning~25m

Problem

Cut a string into pieces so that every piece reads the same forwards and backwards. Return every way of doing that, with each way given as the list of pieces in order. Single characters count as palindromes, so at least one cutting always exists.

Examples

Input:  s = "aab"
Output: [["a", "a", "b"], ["aa", "b"]]
Why:    "ab" is not a palindrome, so no cutting starts with it
Input:  s = "aaa"
Output: 4 cuttings
Why:    "a|a|a", "aa|a", "a|aa" and "aaa" all qualify
Input:  s = "a"
Output: [["a"]]
Why:    edge case, a single character is already a palindrome

Hints

0 / 3

Stuck on the idea rather than the code? Word Search & Pruning covers it.