lintcode-medium-Palindrome Partition II

Given a string s, cut s into some substrings such that every substring is a palindrome.

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

Example

Given s = "aab",

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

public class Solution {
    /**
     * @param s a string
     * @return an integer
     */
    public int minCut(String s) {
        // write your code here

        if(s == null || s.length() == 0)
            return 0;

        int len = s.length();
        boolean[][] isPalindrome = new boolean[len][len];

        for(int i = 0; i < len; i++){
            isPalindrome[i][i] = true;
        }

        for(int i = 0; i < len - 1; i++)
            isPalindrome[i][i + 1] = (s.charAt(i) == s.charAt(i + 1));

        for(int i = len - 1; i >= 0; i--){
            for(int j = i + 2; j < len; j++){
                if(s.charAt(i) == s.charAt(j) && isPalindrome[i + 1][j - 1])
                    isPalindrome[i][j] = true;
                else
                    isPalindrome[i][j] = false;
            }
        }

        if(isPalindrome[0][len - 1])
            return 0;

        int[] f = new int[s.length()];
        for (int i = 0; i < s.length(); i++) {
            f[i] = i;
        }

        for (int i = 0; i < s.length(); i++) {
            if(isPalindrome[0][i])
                f[i] = 0;
            else
                for (int j = 0; j < i; j++)
                    if(isPalindrome[j + 1][i])
                        f[i] = Math.min(f[i], f[j] + 1);
        }

        return f[len - 1];
    }

    public boolean valid(String s){
        int left = 0;
        int right = s.length() - 1;

        while(left < right){
            if(s.charAt(left) != s.charAt(right))
                return false;

            left++;
            right--;
        }

        return true;
    }

};
时间: 2024-10-05 05:02:40

lintcode-medium-Palindrome Partition II的相关文章

LeetCode &quot;Palindrome Partition II&quot; - Double DP

A so juicy and interesting problem! Recommend to everyone! A final solution may not jump into your mind immediately, of course, DP is not a trivial topic. So let's go step by step - it is more interesting that how you get to destination than destinat

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

[LintCode] Palindrome Partitioning II

Palindrome Partitioning II Given a string s, cut s into some substrings such that every substring is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. Example For example, given s = "aab", Return 1 since the palind

[LeetCode]132.Palindrome Partitioning II

题目 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&qu

LeetCode: Palindrome Partitioning II 解题报告

Palindrome Partitioning II 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 pa

【LeetCode】Palindrome Partitioning II

Palindrome Partitioning II  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 p

19. Palindrome Partitioning &amp;&amp; Palindrome Partitioning II (回文分割)

Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a&q

[email&#160;protected] [131/132] Palindrome Partitioning &amp; Palindrome Partitioning II

https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa&qu

[LeetCode] Palindrome Partitioning II [12]

题目 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&qu