AlgoFrames

Palindrome partitioning II

Standard · Write code · about 20 min

Cut the string s into pieces so that every piece is a palindrome (it reads the same both ways).

Return the fewest cuts needed.

Example 1

Input:

["aab"]

Output:

1

"aa" | "b": 1 cut.

Example 2

Input:

["abacdc"]

Output:

1

"aba" | "cdc": 1 cut.

Constraints

  • 1 ≤ s.length ≤ 2000
  • s holds lowercase English letters

Learn the idea first: Interval DP: matrix chain, burst balloons, palindrome partitioning, a visual lesson.

More Interval DP: matrix chain, burst balloons, palindrome partitioning problems