palindrome partition II

中等 分割回文串 II
21%
通过

给定一个字符串s,将s分割成一些子串,使每个子串都是回文。

返回s符合要求的的最少分割次数。

您在真实的面试中是否遇到过这个题? 
Yes
样例

比如,给出字符串s = "aab"

返回 1, 因为进行一次分割可以将字符串s分割成["aa","b"]这样两个回文子串


public class Solution {
    private boolean isPalindrome(String s, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }

    private boolean[][] getIsPalindrome(String s) {
        boolean[][] isPalindrome = new boolean[s.length()][s.length()];

        for (int i = 0; i < s.length(); i++) {
            isPalindrome[i][i] = true;
        }
        for (int i = 0; i < s.length() - 1; i++) {
            isPalindrome[i][i + 1] = (s.charAt(i) == s.charAt(i + 1));
        }

        for (int length = 2; length < s.length(); length++) {
            for (int start = 0; start + length < s.length(); start++) {
                isPalindrome[start][start + length]
                    = isPalindrome[start + 1][start + length - 1] && s.charAt(start) == s.charAt(start + length);
            }
        }

        return isPalindrome;
    }
    public int minCut(String s) {
        // write your code here
        if (s == null || s.length() == 0) {
            return 0;
        }

        int[] cut = new int[s.length() + 1];
        boolean[][] isPalindrome = getIsPalindrome(s);

        cut[0] = 0;
        for (int i = 1; i <= s.length(); i++) {
            cut[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= i; j++) {
                if (isPalindrome[i - j][i - 1] && cut[i - j] != Integer.MAX_VALUE) {
                    cut[i] = Math.min(cut[i], cut[i - j] + 1);
                }
            }
        }

        return cut[s.length()] - 1;
    }
}

/**
 * s="noonlevel"
 * s.length()=9
 * int[] cut = new int[10];
 * boolean[][] isPalindrome = getIsPalindrome(s);
 *
 * isPalindrome = new boolean[9][9];
 * 
 * 0 1 2 3 4 5 6 7 8
 * n o o n l e v e l
 * 
 *   0   1   2   3   4   5   6   7   8
 * [ t][ f][ f][ t][ f][ f][ f][ f][ f]
 * [10][ t][ t][ f][ f][ f][ f][ f][ f]
 * [20][21][ t][ f][ f][ f][ f][ f][ f]
 * [30][31][32][ t][ f][ f][ f][ f][ f]
 * [40][41][42][43][ t][ f][ f][ f][ t]
 * [50][51][52][53][54][ t][ f][ t][ f]
 * [60][61][62][63][64][65][ t][ f][ f]
 * [70][71][72][73][74][75][76][ t][ f]
 * [80][81][82][83][84][85][86][87][ t]
 * isP[0][1] = s.charAt(0) == s.cahrAt(1) ==>f
 * isP[1][2] = s.charAt(1) == s.cahrAt(2) ==>t
 * isP[2][3] = s.charAt(2) == s.cahrAt(3) ==>f
 * isP[3][4] = s.charAt(3) == s.cahrAt(4) ==>f
 * isP[4][5] = s.charAt(4) == s.cahrAt(5) ==>f
 * isP[5][6] = s.charAt(5) == s.cahrAt(6) ==>f
 * isP[6][7] = s.charAt(6) == s.cahrAt(7) ==>f
 * isP[7][8] = s.charAt(7) == s.cahrAt(8) ==>f
 * 
 * l=2, 
 * isP[0][2] = isP[1][1]&&s.charAt(0) == s.cahrAt(2) ==> t&&f ==>f
 * isP[1][3] = isP[2][2]&&s.charAt(1) == s.cahrAt(3) ==> t&&f ==>f
 * isP[2][4] = isP[3][3]&&s.charAt(2) == s.cahrAt(4) ==> t&&f ==>f
 * ...
 * isP[5][7] = isP[6][6]&&s.charAt(5) == s.cahrAt(7) ==> t&&t ==>t
 * ...
 * 
 * l=3
 * isP[0][3] = isP[1][2]&&s.charAt(0) == s.cahrAt(3) ==> t&&t ==>t
 * isP[1][4] = isP[2][3]&&s.charAt(1) == s.cahrAt(4) ==> f&&f ==>f
 * isP[2][5] = isP[3][4]&&s.charAt(2) == s.cahrAt(5) ==> f&&f ==>f
 * ...
 * 
 * l=4
 * isP[0][4] = isP[1][3]&&s.charAt(0) == s.cahrAt(4) ==> f
 * isP[1][5] = isP[2][4]&&s.charAt(1) == s.cahrAt(5) ==> f
 * isP[2][6] = isP[3][5]&&s.charAt(2) == s.cahrAt(6) ==> f
 * isP[3][7] = isP[4][6]&&s.charAt(3) == s.cahrAt(7) ==> f
 * isP[4][8] = isP[5][7]&&s.charAt(4) == s.cahrAt(8) ==> t
 * 
 * l=5
 * isP[0][5] = isP[1][4]&&s.charAt(0) == s.cahrAt(5) ==> f
 * isP[1][6] = isP[2][5]&&s.charAt(1) == s.cahrAt(6) ==> f
 * isP[2][7] = isP[3][6]&&s.charAt(2) == s.cahrAt(7) ==> f
 * isP[3][8] = isP[4][7]&&s.charAt(3) == s.cahrAt(8) ==> f
 * 
 * l=6
 * isP[0][6] = isP[1][5]&&s.charAt(0) == s.cahrAt(6) ==> f
 * isP[1][7] = isP[2][6]&&s.charAt(1) == s.cahrAt(7) ==> f
 * isP[2][8] = isP[3][7]&&s.charAt(2) == s.cahrAt(8) ==> f
 * 
 * l=7
 * isP[0][7] = isP[1][6]&&s.charAt(0) == s.cahrAt(7) ==> f
 * isP[1][8] = isP[2][7]&&s.charAt(1) == s.cahrAt(8) ==> f
 * 
 * l=8
 * isP[0][8] = isP[1][7]&&s.charAt(0) == s.cahrAt(8) ==> f
 * 
 * cut[0] = 0;
        for (int i = 1; i <= s.length(); i++) {
            cut[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= i; j++) {
                if (isPalindrome[i - j][i - 1] && cut[i - j] != Integer.MAX_VALUE) {
                    cut[i] = Math.min(cut[i], cut[i - j] + 1);
                }
            }
        }
        
 *      0  1  2  3  4  5  6  7  8  9  
 * i=1 [0][~][0][0][0][0][0][0][0][0]
 * j=1 isP[0][0] && cut[0]!=~ => cut[1] = min(cut[1],cut[0]+1=1) =1
 *     [0][1][0][0][0][0][0][0][0][0]
 * 
 * i=2 [0][1][~][0][0][0][0][0][0][0]
 * j=1 isP[1][1] && cut[1]!=~ => cut[2] = min(cut[2],cut[1]+1=2) =2
 *     [0][1][2][0][0][0][0][0][0][0]
 * j=2 isP[0][1] && cut[1]!=~ =>
 *     [0][1][2][0][0][0][0][0][0][0]
 * 
 * i=3 [0][1][2][~][0][0][0][0][0][0]
 * j=1 isP[2][2] && cut[2]!=~ => cut[3] = min(cut[3],cut[2]+1=3) =3
 *     [0][1][2][3][0][0][0][0][0][0]
 * j=2 isP[1][2] && cut[1]!=~ => cut[3] = min(cut[3],cut[1]+1=2) =2
 *     [0][1][2][2][0][0][0][0][0][0]
 * j=3 isP[0][2] && cut[0]!=~ => 
 *     [0][1][2][2][0][0][0][0][0][0]
 * 
 * i=4 [0][1][2][2][~][0][0][0][0][0]
 * j=1 isP[3][3] && cut[3]!=~ => cut[4] = min(cut[4],cut[3]+1=3) =3
 *     [0][1][2][2][3][0][0][0][0][0]
 * j=2 isP[2][3] && cut[2]!=~ => 
 * j=3 isP[1][3] && cut[1]!=~ =>
 * j=4 isP[0][3] && cut[0]!=~ => cut[4] = min(cut[4],cut[0]+1=1) =1
 *     [0][1][2][2][1][0][0][0][0][0]
 * 
 * i=5 [0][1][2][2][1][~][0][0][0][0]
 * j=1 isP[4][4] && cut[4]!=~ => cut[5] = min(cut[5],cut[4]+1=2) =2
 *     [0][1][2][2][1][2][0][0][0][0]
 * j=2 isP[3][4] && cut[3]!=~ =>
 * j=3 isP[2][4] && cut[2]!=~ =>
 * j=4 isP[1][4] && cut[1]!=~ =>
 * j=5 isP[0][4] && cut[0]!=~ =>
 * 
 * i=6 [0][1][2][2][1][2][~][0][0][0]
 * j=1 isP[5][5] && cut[5]!=~ => cut[6] = min(cut[6],cut[5]+1=3) =3
 *     [0][1][2][2][1][2][3][0][0][0]
 * j=2 isP[4][5] && cut[4]!=~ =>
 * j=3 isP[3][5] && cut[3]!=~ =>
 * j=4 isP[2][5] && cut[2]!=~ =>
 * j=5 isP[1][5] && cut[1]!=~ =>
 * j=6 isP[0][5] && cut[0]!=~ =>
 * 
 * i=7 [0][1][2][2][1][2][3][~][0][0]
 * j=1 isP[6][6] && cut[6]!=~ => cut[7] = min(cut[7],cut[6]+1=4) =4
 *     [0][1][2][2][1][2][3][4][0][0]
 * j=2 isP[5][6] && cut[5]!=~ =>
 * j=3 isP[4][6] && cut[4]!=~ =>
 * j=4 isP[3][6] && cut[3]!=~ =>
 * j=5 isP[2][6] && cut[2]!=~ =>
 * j=6 isP[1][6] && cut[1]!=~ =>
 * j=7 isP[0][6] && cut[0]!=~ =>
 * 
 * i=8 [0][1][2][2][1][2][3][4][~][0]
 * j=1 isP[7][7] && cut[7]!=~ => cut[8] = min(cut[8],cut[7]+1=5) =5
 *     [0][1][2][2][1][2][3][4][5][0]
 * j=2 isP[6][7] && cut[6]!=~ =>
 * j=3 isP[5][7] && cut[5]!=~ => cut[8] = min(cut[8],cut[5]+1=3) =3
 *     [0][1][2][2][1][2][3][4][3][0]
 * j=4 isP[4][7] && cut[4]!=~ =>
 * j=5 isP[3][7] && cut[3]!=~ =>
 * j=6 isP[2][7] && cut[2]!=~ =>
 * j=7 isP[1][7] && cut[1]!=~ =>
 * j=8 isP[0][7] && cut[0]!=~ =>
 * 
 * i=9 [0][1][2][2][1][2][3][4][3][~]
 * j=1 isP[8][8] && cut[8]!=~ => cut[9] = min(cut[9],cut[8]+1=4) =4
 *     [0][1][2][2][1][2][3][4][3][4]
 * j=2 isP[7][8] && cut[7]!=~ =>
 * j=3 isP[6][8] && cut[6]!=~ => 
 * j=4 isP[5][8] && cut[5]!=~ =>
 * j=5 isP[4][8] && cut[4]!=~ => cut[9] = min(cut[9],cut[4]+1=2) =2
 *     [0][1][2][2][1][2][3][4][3][2]
 * j=6 isP[3][8] && cut[3]!=~ =>
 * j=7 isP[2][8] && cut[2]!=~ =>
 * j=8 isP[1][8] && cut[1]!=~ =>
 * j=8 isP[0][8] && cut[0]!=~ =>
 * return cut[9]-1=2-1=1
<pre name="code" class="java"> 
 *     0  1  2  3  4  5  6  7  8
 *     n  o  o  n  l  e  v  e  l
 *  0  1  2  3  4  5  6  7  8  9
 * [0][1][2][2][1][2][3][4][3][2]

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值