-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
38 lines (34 loc) · 998 Bytes
/
Main.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
35
36
37
38
package com.company;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// write your code here
}
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
dfs(s, new ArrayList<>(), res);
return res;
}
public void dfs(String s, List<String> path, List<List<String>> res){
if(s.isEmpty()){
res.add(new ArrayList<>(path));
return;
}
for (int i = 1; i <= s.length(); i++) {
if(check(s.substring(0, i))){
path.add(s.substring(0, i));
dfs(s.substring(i), path, res);
path.remove(path.size() - 1);
}
}
}
public boolean check(String s){
int li = 0, hi = s.length() - 1;
while (li < hi){
if(s.charAt(li++) != s.charAt(hi--))
return false;
}
return true;
}
}