Problem
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 |
|
Explanation
-
The two dimensional array
isPalindrome[i][j]
means if the string from character of index i to index j is a palindrome. For example, in the string “abba”,isPalindrome[0][3]
will be true because string[0] = string[3] = “a” andisPalindrome[1][2]
is true because string[1]=”b” same as string[2]=”b”. -
The one dimensional array
dp[i]
means the length of i (we begin from 0 to i-1) ‘s string’s minimum cut.dp[i] = min(dp[j] + 1)
if the string starts from index j to i-1 (we divide the string intostr[0:j-1]
andstr[j:n]
the right substring) is palindrome, then the minimum cut of the whole string is equal to the left substring’s minimum cut plus one. We plus one because we need one cut to cut the left substring and right substring.
Solution
1 |
|