Leetcode 131. Palindrome Partitioning-----java

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"]]

Recursive solution, no difficulty.

 Public classSolution {List List=NewArraylist<arraylist<string>>(); Char[] word;    String SS;    String[] result;  PublicList<list<string>>partition (String s) {intLen =s.length (); if(len = = 0)            returnlist; if(len = = 1) {ArrayList<String> L1 =NewArraylist<string>();            L1.add (s);            List.add (L1); returnlist; } SS=s; Word=S.tochararray (); Result=NewString[len];  for(inti = 0;i < len;i++){            if(Ispalindrome (0, i)) {result[0] = s.substring (0,i+1); Helper (i+1,1); }        }        returnlist; }     Public voidHelperintStartintnum) {        if(Start = =word.length) {ArrayList ll=NewArraylist<string>();  for(inti = 0;i<num;i++) Ll.add (Result[i]);            List.add (LL); return ; }         for(inti = start; I < word.length;i++){            if(Ispalindrome (start,i)) {Result[num]= Ss.substring (start,i+1); Helper (i+1,num+1); }        }    }     Public BooleanIspalindrome (intStartintend) {         while(Start <end) {            if(Word[start] = =Word[end]) {Start++; End--; }Else                return false; }        return true; }}

Leetcode 131. Palindrome Partitioning-----java

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.