Subject description:
Given a string s, s is divided into a number of sub-strings, each sub-string is a string palindrome.
S return all the possible partitioning scheme.
Example:
输入: "aab"
输出:
[
["aa","b"],
["a","a","b"]
]
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/palindrome-partitioning
Problem-solving ideas:
1, apparently backtracking: to do everything possible outcomes & the result is a List <list <>> type.
2, so this question and seek subset is very similar to each his own knowledge to be a judge is not a palindrome string.
3, note that the function is determined using the back huisu (int begin, int end, String target) to facilitate processing the string
Java code to achieve the following
1 //回文字符判断 2 public boolean isPalindrome(String s,int start,int end){ 3 while(start<end){ 4 if(s.charAt(start)!=s.charAt(end)) 5 return false; 6 start++; 7 end--; 8 } 9 return true; 10 } 11 12 public List<List<String>> partition(String s) { 13 //求出所有子集 14 //判断是不是回文 15 List<List<String>> result = new ArrayList<>(); 16 List<String> temp = new ArrayList<>(); 17 part(result,temp,s,0); 18 return result; 19 } 20 21 public void part(List<List<String>> result,List<String> temp,String s,int begin){ 22 if (begin==s.length()){ 23 result.add(new ArrayList(temp)); //Must first apply for memory, otherwise it is a reference to the added result is returned is empty. 24- return ; // processing is complete we must return! ! ! 25 } 26 is for ( int I = the begin; I <s.length (); I ++ ) { 27 IF (isPalindrome (S, the begin, I)) { 28 temp.add (s.substring (the begin, I +. 1 )) ; 29 Part (Result, TEMP, S, I +. 1 ); 30 temp.remove (temp.size () -. 1 ); 31 is } 34 is }