Java-palindrome Partitioning

Source: Internet
Author: User

Given a string s, partition s such that every substring of the partition is a Palindrome.

Return all possible palindrome partitioning of s.

For example, given s = "aab" ,
Return

  [    ["AA", "B"],    ["A", "a", "B"]  ]

Decomposes a given string so that each division is a palindrome structure requires all possible combinations to be given

A typical backtracking solution starts at the beginning of the i= (Index,s.length ()-1) If the index-i bit satisfies the palindrome structure, the continuation of recursion from the i+1 bit needs to be noted that the end condition can be index=s.length () that all bits are split. The code is as follows:

public class Solution {     public  list<list<string>> partition (String s) {list<list<string >> res=new arraylist<list<string>> (); List<string>  tmp=new arraylist<string> ();p Art (res, S, tmp, 0); return res;    } Public   void part (list<list<string>> res,string s,list<string>  tmp,int index) {if (index== S.length ()) {res.add (TMP); return;} for (int i=index;i<s.length (); i++) {if (Ispalindrome (S, index, i)) {list<string>  ntmp=new arraylist< String> (); Ntmp.addall (TMP), Ntmp.add (s.substring (index,i+1));p Art (res, S, ntmp, i+1);}} Public  Boolean ispalindrome (String s,int Start,int end) {for (int i=0;i< (end-start+1)/2;i++) {if (S.charat (i+ Start)!=s.charat (end-i)) {return false;}} return true;}    }

Java-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.