Palindrome Partitioning I & II

Source: Internet
Author: User

Given A string s, partition s such that every substring of the partition are a palindrome.

Return all possible palindrome partitioning of S.

Example

Given s = "aab" , return:

[  ["aa","b"],  ["a","a","b"]]
Analysis: Create a method partition to divide a string str into multiple palindrome. How do you do it? Since it is split, the split point is in the middle of every two characters. So, first the STR is divided into two paragraphs, [0, I] and [i + 1, n], see if the first paragraph is palindrome, yes, recursive call partition, and the second paragraph passed in. Then merge the resulting list with the first paragraph.
1  Public classSolution {2     /**3 * @param s:a string4 * @return: A List of lists of string5      */6 7      PublicArraylist<arraylist<string>>partition (String str) {8Arraylist<arraylist<string>> Listall =NewArraylist<arraylist<string>>();9         Ten         if(str==NULL|| Str.length () = =0)returnListall; One  A          for(inti =1; I <= str.length (); i++) { -String StrPart1 = str.substring (0, i); -String StrPart2 =str.substring (i); the  -             if(Ispalindrome (StrPart1)) { -                 if(Strpart1.equals (str)) { -arraylist<string> list =NewArraylist<string>(); + List.add (str); - Listall.add (list); +}Else { Aarraylist<arraylist<string>> temp =partition (STRPART2); at                      for(intj =0; J < Temp.size (); J + +) { -Temp.Get(j). Add (0, strPart1); -Listall.add (NewArraylist<string> (temp.Get(j))); -                     } -                 } -             } in         } -         returnListall; to     } +  -      Publicboolean ispalindrome (String str) { the         if(str = =NULL|| Str.length () = =1) *             return true; $ Panax Notoginseng         inti =0; -         intj = str.length ()-1; the          while(I <j) { +             if(Str.charat (i)! =Str.charat (j)) { A                 return false; the             } +i++; -j--; $         } $         return true; -     } -}
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

Given s = "aab" ,

Return 1 since the palindrome partitioning ["AA", "B"] could be produced using 1 cut.

Analysis:

Cuts[i] Represents the minimum number of cuts from 0 to I. So in order to get cuts[i], we need from 0 to I to see if the string K (0 <= K < i) to Part I is palindrome. Yes, we need to update the value of the current cuts[i] because it is possible for us to find a smaller value at this time.

1  Public classSolution {2     /**3 * @param s a string4 * @return an integer5      */6 7      Public intmincut (String s) {8         if(s = =NULL|| S.length () <=1)return 0;9         Ten         int[] cuts =New int[S.length () +1]; One          for(inti =1; i < cuts.length; i++) { ACuts[i] =i; -         } -cuts[0] =0; the          -          for(inti =1; i < cuts.length; i++) { -              for(intj =1; J <= I; J + +) { -                 if(Ispalindrome (S, J-1I1)) { +Cuts[i] = Math.min (Cuts[i], cuts[j-1] +1); -                 } +             } A         } at         returncuts[s.length ()]; -     } -      -      PublicBoolean ispalindrome (String str,intStartintend) { -          while(Start <end) { -             if(Str.charat (start)! =Str.charat (end)) { in                 return false; -             } tostart++; +end--; -         } the         return true; *     } $}

Palindrome Partitioning I & 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.