-
Notifications
You must be signed in to change notification settings - Fork 1
/
Solution.java
39 lines (32 loc) · 1022 Bytes
/
Solution.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
39
package palindrome_partitioning;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
class Solution {
private boolean isPalindrome(String s, int start, int end) {
while (start < end) {
if (s.charAt(start++) != s.charAt(end--)) {
return false;
}
}
return true;
}
private void partition(String s, Stack<String> stack, List<List<String>> result, int start) {
if (start == s.length()) {
result.add(new ArrayList<>(stack));
return;
}
for (int i = start; i < s.length(); i++) {
if (isPalindrome(s, start, i)) {
stack.add(s.substring(start, i + 1));
partition(s, stack, result, i + 1);
stack.pop();
}
}
}
public List<List<String>> partition(String s) {
List<List<String>> result = new ArrayList<>();
partition(s, new Stack<>(), result, 0);
return result;
}
}