[Leedcode 131] Palindrome Partitioning

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.

For example, given s = "aab" ,
Return

  [    ["AA", "B"],    ["A", "a", "B"]  ]
 Public classSolution {//Dfs idea, the Getpart function means starting at the start index of S to find a palindrome, the DFS process is, take the start of the I character, if it is a palindrome, save the results, from the start+i began to traverseList<list<string>>Res; List<String>seq;  PublicList<list<string>>partition (String s) {res=NewArraylist<list<string>>(); Seq=NewArraylist<string>(); GetPart (s),0); returnRes; }     Public voidGetPart (String S,intstart) {        if(start==s.length ()) {Res.add (NewArraylist<string>(seq)); }         for(intI=start+1;i<=s.length (); i++){            if(Ispart (s.substring (start,i))) {Seq.add (s.substring (start,i));                GetPart (S,i); Seq.remove (Seq.size ()-1); }        }    }     Public BooleanIspart (String s) {intI=0; intJ=s.length ()-1;  while(i<j) {            if(S.charat (i)!=s.charat (j))return false; I++; J--; }        return true; }        }

[Leedcode 131] Palindrome Partitioning

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.