leetcode--Palindrome Partitioning
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"] ]
public class Solution { public ArrayList<ArrayList<String>> partition(String s) { ArrayList<ArrayList<String> > result = new ArrayList<ArrayList<String> >(); int len = s.length(); if(len > 0){ for(int i = 0; i < len; ++i){ if(isPalindrome(s.substring(0, i + 1))){ ArrayList<ArrayList<String> > temp = partition(s.substring(i + 1, len)); int size = temp.size(); if(size > 0){ while(!temp.isEmpty()){ ArrayList<String> aPartition = new ArrayList<String>(); aPartition.add(s.substring(0, i + 1)); ArrayList<String> alist = temp.remove(0); aPartition.addAll(alist); result.add(aPartition); } } else{ ArrayList<String> alist = new ArrayList<String>(); alist.add(s.substring(0,i + 1)); result.add(alist); } } } } return result; } public boolean isPalindrome(String s){ boolean isPal = true; for(int i = 0,j = s.length() - 1; i <= j; ++i, --j){ if(s.charAt(i) != s.charAt(j)){ isPal = false; break; } } return isPal; } }