132. Palindrome Partitioning II

本文介绍了一种算法,用于计算将字符串分割成回文子串所需的最少切割次数,并提供了两种解决方案,一种直接检查所有可能的回文子串,另一种通过预处理避免重复计算提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

For example, given s = "aab",

Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.


The idea is to maintain a result from i = 0  to (i - 1)

when the ith letter comes in dp[i] = min(dp[j - 1] + 1) for all the j that s(j -> i) is a palindrome .

Code:

public class Solution {
    public int minCut(String s) {
        int[] dp = new int[s.length() + 1];
        for(int i = 1; i < dp.length; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = i; j > 0; j--){
                if(isPanlindrome(s,j - 1,i - 1)){
                    dp[i] = Math.min(dp[i],1 + dp[j - 1]);
                }
            }
        }
        return dp[dp.length - 1] - 1;
        
        
    }
    
    public boolean isPanlindrome(String s, int start, int end){
        while(start < end){
            if(s.charAt(start) == s.charAt(end)){
                start++;
                end--;
            } else {
                return false;
            }
        }
        return true;
    }
}

Time complexity O(N3). since
isPanlindrome() takes O(n)


This code will get TLE on Leetcode since for isPanlindrome part, there are many redundant computation. 

For example "baaab" we already know that in the first "baaa" the aaa is a panlidrome. when the last b comes in , we should only check "b***b" and the info about "aaa" should be stored to improve the performance.

if s(i) != s(j)     p[i][j] = false;

else p[i][j] = p[i - 1][j + 1].

code:

public class Solution {
    public int minCut(String s) {
        int[] dp = new int[s.length() + 1];
        boolean[][] p = new boolean[s.length()][s.length()];
        for(int i = 0 ; i < s.length(); i++){
            p[i][i] = true;
        }
        for(int i = 0 ; i < s.length() - 1; i++){
            p[i][i+1] = (s.charAt(i) == s.charAt(i+1));
        }
        
        for(int l = 2; l < s.length(); l++){
            for(int i = 0 ; i < s.length() - l; i++){
                int j = i + l;
                if(s.charAt(i) == s.charAt(j)){
                    p[i][j] = p[i + 1][j - 1];
                }
            }
        }
        
        
        for(int i = 1; i < dp.length; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = i; j > 0; j--){
                if(p[j - 1][i - 1]){
                    dp[i] = Math.min(dp[i],1 + dp[j - 1]);
                }
            }
        }
        return dp[dp.length - 1] - 1;
        
        
    }

}


The pre-process of the data is important to avoid TLE, total Time O(n2).





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值