題目:
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 {
List<List<String>> result = new ArrayList<>();
List<String> path = new ArrayList<>();
public List<List<String>> partition(String s) {
helper(s);
return result;
}
void helper(String s)
{
if(s.length() == )
{
List<String> r = new ArrayList<>(path);
result.add(r);
}
for(int i = ;i<s.length();i++)
{
String str = s.substring(,i+);
if(isPoalindrome(str))
{
path.add(str);
helper(s.substring(i+));
path.remove(path.size()-);
}
}
}
boolean isPoalindrome(String s)
{
int i = ,j = s.length()-;
while(i<=j)
{
if(s.charAt(i) != s.charAt(j)){return false;}
i++;
j--;
}
return true;
}
}