Title:
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"] ]
Test Instructions:
Given a string s, divide the string so that each partition is a palindrome string.
Returns the division of all palindrome strings for S.
For example, given s = "aab" ,
Return
[ ["AA", "B"], ["A", "a", "B"] ]
Algorithm Analysis:
Backtracking recursion, thinking more clear, look at the code better understanding.
AC Code:
<span style= "Font-family:microsoft yahei;font-size:12px;" >public class Solution {public arraylist<arraylist<string>> partition (String s) {arraylist< arraylist<string>> list = new arraylist<arraylist<string>> (); arraylist<string> tmp = new arraylist<string> (); Getrs (list, TMP, s); return list;} public void Getrs (arraylist<arraylist<string>> list,arraylist<string> tmp,string s) {if (S.length () ==0| | S==null) {List.add (new arraylist<string> (TMP)); return;} else{for (int i = 1;i<=s.length (); i++) {if (Ispalindrome (s.substring (0, I))//This substring starts at the specified beginindex until the index endIndex- 1 characters {tmp.add (s.substring (0, I)); Getrs (list, TMP, s.substring (i));//The substring starts at the character at the specified index until the end of the string. Tmp.remove (Tmp.size ()-1);}}}} Determines whether a string is a palindrome public boolean ispalindrome (string s) {if (s==null| | S.length () ==0) return true;for (int i = 0,J = S.length () -1;i<j;i++,j--) {if (S.charat (i)!=s.charat (j)) return false;} return true;} }</span>
Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source
[Leetcode] [Java] Palindrome Partitioning