132. Palindrome Partitioning II

Description

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

Example 1:

Input: s = "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.

Example 2:

Input: s = "a"
Output: 0

Constraints:

  • 1 <= s.length <= 2000

  • s consists of lower-case English letters only.

Tags

Dynamic Programming

Solution

We denote f[i]f[i] as the least number of cuts s[0..i] needs. We can formulate its transition function as below:

f[i]=minf[j]+1,0ji,s[j+1..i]isapalindromef[i] = \min {f[j]}+1, 0\leq j\leq i, s[j+1..i] \,is\,a\,palindrome

There is an edge case leads f[i]=0f[i]=0 when s[0..i]isapalindromes[0..i] \,is\,a\,palindrome .

We also use a 2D array to determine every substring that whether it is a palindrome, with the dynamic programming solver below:

Complexity

  • Time complexity: O(n)O(n)

  • Space complexity: O(n)O(n)

Code

Reference

Last updated

Was this helpful?