-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPartition.java
More file actions
executable file
·44 lines (32 loc) · 1.09 KB
/
Copy pathPartition.java
File metadata and controls
executable file
·44 lines (32 loc) · 1.09 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
class Solution {
public boolean isPalindrome(String s){
int n = s.length();
for (int i = 0 ; i < (n / 2); i++){
if (s.charAt(i) != s.charAt(n - i -1)){
return false;
}
}
return true;
}
List<List<String>> output = new ArrayList<>();
public void getPartition(String s, List<String> res){
if (s.isEmpty()){
output.add(res);
return;
}
for (int i = 0; i < s.length(); i++){
String prefix = s.substring(0, i + 1);
String suffix = s.substring(i + 1);
if (isPalindrome(prefix)){
List<String> tempRes = new ArrayList<>(res);
tempRes.add(prefix);
getPartition(suffix, tempRes);
}
}
}
public List<List<String>> partition(String s) {
List<String> res = new ArrayList<>();
getPartition(s, res);
return output;
}
}