Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

s =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Backtrack all cut positions, only cutting at palindromic substrings.

Approach

Precompute isPal[l][r] via DP. Then DFS from index i, try every j >= i where s[i..j] is palindrome, append it, and recurse at j+1.

Why it works

Every valid partition is a sequence of palindromic segments; DP allows O(1) palindrome checks during backtracking.

Code
Loading...
Complexity
Time: O(n^2 + n*2^n) (output-sensitive)
Space: O(n^2)
Solution
Updated: 2026-02-23

Idea

Backtrack all cut positions, only cutting at palindromic substrings.

Approach

Precompute isPal[l][r] via DP. Then DFS from index i, try every j >= i where s[i..j] is palindrome, append it, and recurse at j+1.

Why it works

Every valid partition is a sequence of palindromic segments; DP allows O(1) palindrome checks during backtracking.

Code
Loading...
Complexity
Time: O(n^2 + n*2^n) (output-sensitive)
Space: O(n^2)