cut only where the prefix is a palindrome
a|a|b
a|a
a|ab
a
aa|b
aa
aab
ε
partitions
(none yet)
Backtracking · cut on palindrome prefixes only
▸1partition(pos, parts):2 if pos == len(s): record parts; return // reached the end3 for end in pos+1..len(s):4 prefix = s[pos..end]5 if not isPalindrome(prefix): prune; continue6 parts.push(prefix) // choose7 partition(end, parts) // explore8 parts.pop() // un-choose9 return10// answer = all recorded partitions
state
- s"aab"
- goalall-palindrome partitions