-
Notifications
You must be signed in to change notification settings - Fork 0
/
P53.java
34 lines (30 loc) · 880 Bytes
/
P53.java
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
import java.util.*;
import java.io.*;
public class P53 {
static List<List<String>> res;
static boolean isp(String s) {
int i = 0, j = s.length() - 1;
while (i < j)
if (s.charAt(i++) != s.charAt(j--))
return false;
return true;
}
static void solver(String s, int i, int j, ArrayList<String> temp) {
if (i == j) {
res.add(new ArrayList(temp));
}
for (int k = i; k < j; k++) {
String str = s.substring(i, k + 1);
if (isp(str)) {
temp.add(str);
solver(s, k + 1, j, temp);
temp.remove(temp.size() - 1);
}
}
}
public static List<List<String>> partition(String s) {
res = new ArrayList<>();
solver(s, 0, s.length(), new ArrayList<>());
return res;
}
}