Skip to content

Commit 514ed29

Browse files
committed
commit changes to gh-pages aug 23
1 parent 749eb78 commit 514ed29

File tree

5 files changed

+87
-38
lines changed

5 files changed

+87
-38
lines changed
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1+
## [Longest Palindromic Substring](../longest-palindromic-substring) + [Word Break](../word-break)
12

2-
## TODO
3-
* write down thinking
3+
After running [Longest Palindromic Substring](../longest-palindromic-substring),
4+
we got a table `P[len][index]`, that is, we can tell if `s[i..j]` is palindromic
45

6+
## Apply [Word Break](../word-break)
7+
8+
`M[i]` means the mincut of `s[0..i]`.
9+
10+
When a new char comes, we have 3 choices:
11+
12+
* `M[i + 1] = 0`: if `s[0..i + 1]` is palindromic
13+
* `M[i + 1] = M[i] + 1`: cut at `i`, between the old string `s[0..i]` and the new char.
14+
* `M[i + 1] = M[j] + 1`: cut at `j`, `j from 0 -> i`, such that `s[j..i+1]` is palindromic, between the `s[0..j]` and `s[j..i + 1]`.
15+
16+
Our job is to find the minimum value among them.
Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
11
public class Solution {
2-
32
public int minCut(String s) {
4-
final char[] S = s.toCharArray();
5-
final int N = S.length;
3+
char[] S = s.toCharArray();
64

7-
if(N == 0) return 0;
8-
if(N == 1) return 0;
9-
if(N == 2) return S[0] == S[1] ? 0 : 1;
5+
if(S.length == 0) return 0;
106

11-
boolean[][] P = new boolean[N][N];
7+
boolean[][] P = new boolean[S.length][S.length];
128

13-
for(int i = 0; i < N - 1; i++){
14-
P[i][i] = true;
15-
P[i][i + 1] = S[i] == S[i + 1];
16-
}
9+
// len 1
10+
Arrays.fill(P[1 - 1], true);
1711

18-
P[N - 1][N - 1] = true;
12+
// len 2
13+
for(int i = 0; i < S.length - 1; i++){
14+
P[2 - 1][i] = S[i] == S[i + 2 - 1];
15+
}
1916

20-
for(int j = 2; j < N; j++){
21-
for(int i = 0; i < j; i++){
22-
P[i][j] |= P[i + 1][j - 1] && S[i] == S[j];
17+
// len 3 to max
18+
for(int len = 3; len <= S.length; len++){
19+
20+
for(int i = 0; i < S.length - (len - 1); i++){
21+
P[len - 1][i] = P[len - 1 - 2][i + 1] && S[i] == S[i + len - 1];
2322
}
2423
}
2524

26-
27-
int mincut[] = new int[N + 1]; // i -> end
28-
29-
for(int i = N - 2; i >= 0; i--){
30-
31-
if(P[i][N - 1]) {
32-
mincut[i] = 0;
33-
} else {
34-
mincut[i] = mincut[i + 1] + 1;
35-
36-
for(int j = i + 1; j < N; j++){
37-
if(P[i][j]){
38-
mincut[i] = Math.min(1 + mincut[j + 1], mincut[i]);
25+
int[] mincut = new int[S.length + 1];
26+
27+
mincut[0] = 0;
28+
mincut[1] = 0;
29+
30+
for(int len = 2; len <= S.length; len++){
31+
32+
if(P[len - 1][0]){
33+
mincut[len] = 0;
34+
}else{
35+
36+
mincut[len] = mincut[len - 1] + 1;
37+
38+
for(int i = 1; i < len - 1; i++){
39+
40+
if(P[len - i - 1][i]){
41+
mincut[len] = Math.min(mincut[i] + 1 , mincut[len]);
3942
}
4043
}
41-
//mincut = Math.min(minCut(s.substring(i + 1)) + 1, mincut);
4244
}
4345
}
44-
45-
return mincut[0];
4646

47+
return mincut[S.length];
4748
}
48-
}
49+
}
Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
1+
## [Space-time tradeoff](http://en.wikipedia.org/wiki/Space%E2%80%93time_tradeoff)
12

2-
## TODO
3-
* write down thinking
3+
A brute force solution is easy. Yet, obviously, time limited.
44

5+
### Check concatenation using count sum
6+
7+
cut a string `S` into N substring, lets say `s1 * 1, s2 * 2, s3 * 1`,
8+
that is, `S` could be `s2s1s2s3` or `s3s1s2s2` or some string else.
9+
10+
convert `L` to count sum, which is a map of `string to count`
11+
12+
```
13+
M = empty Map
14+
15+
for s in L
16+
M[s] = M[s] + 1
17+
18+
19+
```
20+
21+
22+
check concatenation S by minus count
23+
24+
```
25+
cut S into substrings by some length
26+
27+
_M = clone of M
28+
29+
for s in substrings
30+
31+
if _M[s] == 0
32+
Not a concatenation
33+
34+
_M[s] = _M[s] - 1
35+
36+
37+
38+
```

palindrome-partitioning-ii/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
layout: solution
33
title: Palindrome Partitioning II
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-22 20:14:10 +0800
5+
eaten: true
56
---
67
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
78
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}

substring-with-concatenation-of-all-words/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
layout: solution
33
title: Substring with Concatenation of All Words
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-23 00:06:08 +0800
5+
eaten: true
56
---
67
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
78
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}

0 commit comments

Comments
 (0)