00:00

Problem Statement

Input/Output Examples

Visual Explanation

The last piece of any valid partition is a palindrome ending at the last character. Trying every such palindrome and adding one cut to the best answer for the prefix before it gives the optimum.

Approaches

Both approaches compute cuts[j] for every prefix. They differ in how they find the palindromes ending at each position.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the length of the string. Both approaches examine O(n²) palindromes; the table version also stores n² booleans.

Interview Tips and Recommended Approach

  • Avoid plain recursion. Trying every partition is exponential.
  • Separate the two questions. "Is this piece a palindrome?" and "what is the fewest cuts?" each have their own table.
  • Initialise with the worst case. cuts[j] = j is always achievable.
  • Know the sibling problem. Listing all palindrome partitions needs backtracking and has exponential output.

Recommended Approach: Cuts DP With a Palindrome Table

Why: it combines the palindrome check and the cut count in one clear pass, and every step can be traced by hand on small inputs.