-
Notifications
You must be signed in to change notification settings - Fork 16
/
PalindromePartitioning.java
30 lines (26 loc) · 1.11 KB
/
PalindromePartitioning.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
// https://leetcode.com/problems/palindrome-partitioning
// T: O(n * 2^n)
// S: O(n) for recursion call stack
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class PalindromePartitioning {
public List<List<String>> partition(String s) {
final int length = s.length();
final boolean[][] dp = new boolean[length][length];
final List<List<String>> result = new ArrayList<>();
addValidPartitions(s, 0, dp, result, new LinkedList<>());
return result;
}
private void addValidPartitions(String s, int start, boolean[][] dp, List<List<String>> result, LinkedList<String> current) {
if (start == s.length()) result.add(new ArrayList<>(current));
for (int end = start ; end < s.length() ; end++) {
if (s.charAt(start) == s.charAt(end) && (end - start <= 2 || dp[start + 1][end - 1])) {
dp[start][end] = true;
current.addLast(s.substring(start, end + 1));
addValidPartitions(s, end + 1, dp, result, current);
current.pollLast();
}
}
}
}