dp
with dimensions n x n
, where n
is the length of the string s
, to store palindrome information.dp
array with true
for all indices where i == j
(single characters are palindromes) and for all indices where s[i] == s[j]
and dp[i+1][j-1]
is true
(the characters at both ends match and the substring between them is a palindrome).backtrack(start, path)
that explores all possible partitions starting from index start
.start
reaches the end of the string, add the current path
to the result.i
from start
to the end of the string, check if s[start...i]
is a palindrome by referring to dp[start][i]
.path
and recursively call backtrack(i+1, path)
.path
.backtrack(0, [])
to start the backtracking process.