[Leetcode] [JAVA] 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.

A very difficult problem, not to see the discussion can hardly accept.

The solution seems to be basically DP, unlike the general DP, which requires a DP recording of two features.

1. Use Dp[i] to record the number of s.substring (0,i) and the initial value is dp[i]=i-1. 0<=i<=s.length ().

For each I, you can find at least one J, 0<=j<=i-1, so that s.substring (J,i) is a palindrome string. Find all of these J's set J. The transfer function is: dp[i] = min (dp[j]+1) (J∈J)

The only way to do this is to time out, so you have to continue optimizing. The repetition of the current algorithm is that each time s.substring (j,i) is judged to be a palindrome string, the string needs to be traversed, so the second DP record s.substring (J,I) is a palindrome string.

2. Use Isp[i][j] to record whether s.substring (I,J) is a palindrome string. 0<=i<=s.length (), 0<=j<=s.length (). Initial state isp[i][i]=true (0<=i<=s.length ()), Isp[i][i+1]=true (0<=i<s.length ()).

(PS, not initialized in the code isp[i][i+1], because in the traversal process to make a special judgment (I-j<2 must be True))

In this case, to determine whether s.substring (I,J) is a palindrome string, only need isp[i+1][j-1] is true and S.charat (I-1) ==s.charat (j).

At the same time we can also understand that the order of traversal should be gradually i,j distance elongated.

So there should be two layers of loops, the first layer of loops is used to record dp[i], I from 1 to S.length ().

Second layer loop record Isp[j][i] (I fixed), J from i-1 to 0, reverse traversal.

Last Dp[s.length ()] is the result

The code is as follows:

1      Public intmincut (String s) {2         int[] DP =New int[S.length () +1];3         Boolean[] IsP =New Boolean[S.length () +1] [S.length () +1];4          for(intI=0;i<=s.length (); i++)5         {6isp[i][i]=true;7Dp[i]=i-1;8         }9          for(intI=1;i<=s.length (); i++)Ten              for(intj=i-1;j>=0;j--) One                 if(I-j<2 | | (Isp[j+1][i-1] && s.charat (i-1) = =S.charat (j))) { Aisp[j][i]=true; -Dp[i] = Math.min (Dp[i], dp[j]+1); -                 } the         returndp[s.length ()]; -}

[Leetcode] [JAVA] Palindrome Partitioning II

Related Article

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.