-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
46 lines (41 loc) · 1.08 KB
/
Copy pathSolution.java
File metadata and controls
46 lines (41 loc) · 1.08 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
40
41
42
43
44
45
46
import java.util.ArrayList;
import java.util.List;
public class Solution
{
public List<List<String>> partition(String s)
{
List<List<String>> result = new ArrayList<List<String>>();
getPartitions(s, result, 0, new ArrayList<>());
return result;
}
private void getPartitions(String s, List<List<String>> result, int idx, List<String> arrayList)
{
if (idx >= s.length())
{
result.add(new ArrayList<>(arrayList));
return;
}
for (int i = idx; i < s.length(); i++)
{
if (isPalindrome(s, idx, i))
{
arrayList.add(s.substring(idx, i + 1));
getPartitions(s, result, i + 1, arrayList);
arrayList.remove(arrayList.size() - 1);
}
}
}
private boolean isPalindrome(String s, int i, int j)
{
while (i < j)
{
if (s.charAt(i) != s.charAt(j))
{
return false;
}
i++;
j--;
}
return true;
}
}