-
Notifications
You must be signed in to change notification settings - Fork 92
/
PalindromePartitioning131.java
84 lines (72 loc) · 2.28 KB
/
PalindromePartitioning131.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Given a string s, partition s such that every substring of the partition is
* a palindrome.
*
* Return all possible palindrome partitioning of s.
*
* Example:
*
* Input: "aab"
* Output:
* [
* ["aa","b"],
* ["a","a","b"]
* ]
*/
public class PalindromePartitioning131 {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
helper(s.toCharArray(), 0, new ArrayList<>(), res);
return res;
}
private void helper(char[] chars, int start, List<String> path, List<List<String>> res) {
if (start == chars.length) {
res.add(new ArrayList<>(path));
return;
}
for (int j=start; j<chars.length; j++) {
if (isPalindrome(chars, start, j)) {
path.add(new String(Arrays.copyOfRange(chars, start, j+1)));
helper(chars, j+1, path, res);
path.remove(path.size()-1);
}
}
}
private boolean isPalindrome(char[] chars, int i, int j) {
if (i > j) return true;
if (i == j) return true;
for (int k=0; k<=(j-i)/2; k++) {
if (chars[i+k] != chars[j-k]) return false;
}
return true;
}
/**
* https://leetcode.com/problems/palindrome-partitioning/discuss/41982/Java-DP-+-DFS-solution
*/
public List<List<String>> partition2(String s) {
List<List<String>> res = new ArrayList<>();
boolean[][] dp = new boolean[s.length()][s.length()];
for(int i = 0; i < s.length(); i++) {
for(int j = 0; j <= i; j++) {
if(s.charAt(i) == s.charAt(j) && (i - j <= 2 || dp[j+1][i-1])) {
dp[j][i] = true;
}
}
}
helper(res, new ArrayList<>(), dp, s, 0);
return res;
}
private void helper(List<List<String>> res, List<String> path, boolean[][] dp, String s, int pos) {
if(pos == s.length()) {
res.add(new ArrayList<>(path));
return;
}
for(int i = pos; i < s.length(); i++) {
if(dp[pos][i]) {
path.add(s.substring(pos,i+1));
helper(res, path, dp, s, i+1);
path.remove(path.size()-1);
}
}
}
}