[Leetcode] Palindrome Partitioning II

Source: Internet
Author: User

Given A string s, partition s such that every substring of the partition are 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.

First DP Find all the sub-palindrome string stored in F, and then use the array h to record the minimum number of partitions in a position, is also the DP


public class Solution {Boolean F[][];int res = integer.max_value;int h[];p ublic int mincut (String s) {f = new Boolean[s.le Ngth ()][s.length ()];for (int i = 0; i < s.length (); i++) {f[i][i] = true;}  int maxlen = 1;for (int i = 0; i < s.length ()-1; i++) {if (S.charat (i) = = S.charat (i + 1)) {f[i][i + 1] = True;maxlen = 2;}} for (int len = 3; Len <= s.length (), len++) {Boolean in = false;for (int i = 0; i < s.length ()-len + 1; i++) {if ( S.charat (i) = = S.charat (i + len-1) && f[i + 1][i + len-2]) {f[i][i + len-1] = True;in = true;}} if (in) {maxlen = Len;}} if (MaxLen = = S.length ()) return 0;h = new Int[s.length () +1];h[0] = -1;for (int i=1;i<=s.length (); i++) {H[i] = Integer.max _value;for (int j=1;j<=i;j++) {if (F[j-1][i-1]&&h[j-1]!=integer.max_value) {H[i] = Math.min (H[i], h[j-1]+1) ;}}} Return H[s.length ()];}}


[Leetcode] Palindrome Partitioning II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.