Leetcode Note palindrome partitioning II (IQ crush)

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.

This is the smallest ah, the biggest ah, the longest Ah, etc must be using dynamic planning undoubtedly.

I already know how to judge a string s, S[I...J] is a palindrome.

ISPALINDROME[I][J] = True iff s[i] = = S[j] && ispalindrome[i + 1][j-1] or j-i <= 1

When we get this matrix, we can think of this as an adjacency matrix, which is a graph. Then this problem can be converted into a source shortest path problem. Then the simplest is to use BFS.

 Public intmincut (String s) {Boolean[] Ispalindrome =New Boolean[S.length () + 1] [S.length () + 1]; //Map<integer, set<integer>> adjecent = new hashmap<> ();         for(inti = 0; I <= s.length (); i++) {Ispalindrome[i][i]=true; //Adjecent.put (i, New hashset<integer> ());        }         for(inti = S.length ()-1; I >= 0; i--) {             for(intj = i + 1; J <= S.length (); J + +) {Ispalindrome[i][j]= S.charat (i) = = S.charat (j-1)                        && (j-i = = 1 | | Ispalindrome[i + 1][j-1]); if(Ispalindrome[i][j]) {//Adjecent.get (i). Add (j);} }} LinkedList<Integer> queue =NewLinkedlist<integer>(); Boolean[] visited =New Boolean[S.length () + 1]; Queue.add (0); intHead = 0; intDepth = 0;  while(!Queue.isempty ()) {            intW =Queue.poll (); if(Head = =W) {depth++; Head=-1; }             for(inti = 0; i < ispalindrome[w].length; i++) {                if(Ispalindrome[w][i] && i = =s.length ()) {                    returnDepth-1; }                if(Ispalindrome[w][i] &&!Visited[i]) {Visited[i]=true;                    Queue.add (i); if(head = =-1) {Head=i; }                }            }        }        return-1; }

The result of this is very depressed, because I have been using the hash map to represent the adjacency matrix, the result of the old timeout, so simply to rough, directly using the resulting two-dimensional array to do, unexpectedly over. Hash map should give me O (1) Time to Ah, why not as the array?

So I Google a bit, the results found that the smallest cut actually can also use dynamic planning to do. Forget it, cry out in the toilet.

Look at the code first.

 Public intmincut (String s) {Boolean[] Ispalindrome =New Boolean[S.length ()][s.length ()]; int[] Cut =New int[S.length ()];  for(intj = 0; J < S.length (); J + +) {Cut[j]=J;  for(inti = 0; I <= J; i++) {                if(S.charat (i) = =S.charat (j)&& (j-i <= 1 | | ispalindrome[i + 1][j-1])) {Ispalindrome[i][j]=true; if(I > 0) {Cut[j]= Math.min (Cut[j], cut[i-1] + 1); } Else{Cut[j]= 0; }                }            }        }        returnCut[s.length ()-1]; }

This is just a few lines. It can be observed that the part of the palindrome matrix is the same. The key is the cut array.

CUT[J] represents the minimum cut number of S[0...J].

In the process of I moving, I is used as the dividing point.

Then cut[j] = min (cut[i-1] + 1) IIf S[I...J] is a palindrome for i = 0 ~ J

So in the innermost loop (i from 0 to J), it can be obtained ispalindrome[0...j][j], but also can be obtained cut[j].

The algorithm also has a point, that is, the choice of circular direction is very reasonable.

Leetcode Note palindrome partitioning II (IQ crush)

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.