132.分割回文串II
132.分割回文串II
题解
//state: dp[i]表示string[0,i)最少分割次数
//function: dp[i] = min(dp[i],dp[j]+1) -->j<i && [j+1,i)是回文串
//intialize:dp[i] = i-1
//answer: dp[len(s)]
代码
package main func minCut(s string) int { dp := make([]int, len(s)+1) for i := 0; i <= len(s); i++ { dp[i] = i - 1 for j := 0; j < i; j++ { if isPalindrome(s, j, i-1) { dp[i] = min(dp[i], dp[j]+1) } } } return dp[len(s)] } func isPalindrome(s string, i, j int) bool { for i < j { if s[i] == s[j] { i++ j-- } else { return false } } return true } func min(a, b int) int { if a > b { return b } return a }