-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome Partitioning
More file actions
39 lines (39 loc) · 1.28 KB
/
Palindrome Partitioning
File metadata and controls
39 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Solution {
public ArrayList<ArrayList<String>> partition(String s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
ArrayList<String> container = new ArrayList<String>();
helpler(result, container, s, 0);
return result;
}
private boolean valid(String s, int start, int end)
{
while(end>start)
{
if(s.charAt(start) != s.charAt(end))
return false;
start++;
end--;
}//end while
return true;
}
private void helpler(ArrayList<ArrayList<String>> result, ArrayList<String> container, String s, int start)
{
if(start>=s.length())
{
ArrayList<String> copy = new ArrayList<String>();
copy.addAll(container);
result.add(copy);
}//end if
for(int i=start; i<s.length(); i++)
{
if(valid(s, start, i))
{
container.add(s.substring(start, i+1));
helpler(result, container, s, i+1);
container.remove(container.size()-1);
}
}//end for
}
}