-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-132-Palindrome-Partitioning-II.java
More file actions
101 lines (80 loc) · 3.37 KB
/
LeetCode-132-Palindrome-Partitioning-II.java
File metadata and controls
101 lines (80 loc) · 3.37 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
LeetCode: https://leetcode.com/problems/palindrome-partitioning-ii/
LintCode:
JiuZhang: http://www.jiuzhang.com/solutions/palindrome-partitioning-ii/
ProgramCreek: http://www.programcreek.com/2014/04/leetcode-palindrome-partitioning-ii-java/
Others: http://blog.csdn.net/ljphhj/article/details/22573983
Analysis:
DP problem.
We need two array,
1.adjancy matrix of state, state[left][right] = true means s.substring(left, right + 1) is palindrome
2.function array, f[i] means the minimum # of cuts of s.substring(0, i + 1)
Two situations the substring is a palindrome:
1.s.charAt(left) == s.charAt(right) && right - left <=1
2.s.charAt(left) == s.charAt(right) && state[left + 1][right - 1]
these two situations are combined as s.charAt(left) == s.charAt(right) && (right - left <= 1 || state[left + 1][right - 1])
We can calculate state matrix first, or we can calculate it during calculating f[]
Time complexity: O(n^2)
*/
public class Solution {
// public int minCut(String s) {
// if(s == null) return 0;
// int len = s.length();
// // state
// boolean[][] state = new boolean[len][len];
// //function
// int[] f = new int[len];
// for(int right = 0; right < len; right++){
// // initialize function
// f[right] = right;
// for(int left = 0; left <= right; left++){
// if(s.charAt(left) == s.charAt(right) && (right - left <= 1 || state[left + 1][right - 1])){
// state[left][right] = true; // initialize state
// if(left > 0) f[right] = Math.min(f[right], f[left - 1] + 1);
// else f[right] = 0;
// }
// }
// }
// return f[len - 1];
// }
//Calculating state matrix first.
public int minCut(String s) {
if(s == null) return 0;
int len = s.length();
//state
boolean[][] state = initializeState(s);
//function, # of min cut
int[] f = new int[len];
//initialize function
f[0] = 0;
for(int i = 1; i < len; i++) f[i] = i;
//calculate function
for(int right = 1; right < len; right++){
for(int left = 0; left <= right; left++){
if(state[left][right]) {
if(left > 0) f[right] = Math.min(f[right], f[left - 1] + 1);
else f[right] = 0;
}
}
}
return f[len - 1];
}
private boolean[][] initializeState(String s){
int len = s.length();
boolean[][] state = new boolean[len][len];
//calculate state matrix, 3 steps
//1.length == 0
for(int i = 0; i < len; i++) state[i][i] = true;
//2.length == 1
for(int i = 0; i < len - 1; i++){
if(s.charAt(i) == s.charAt(i + 1)) state[i][i + 1] = true;
}
//3. 1 < length < len
for(int length = 2; length < len; length++){
for(int start = 0; start + length < len; start++){
if(s.charAt(start) == s.charAt(start + length) && state[start + 1][start + length - 1]) state[start][start + length] = true;
}
}
return state;
}
}