Skip to content

Commit b663bc8

Browse files
committed
commit changes to gh-pages aug 19
1 parent 49470f2 commit b663bc8

File tree

5 files changed

+101
-34
lines changed

5 files changed

+101
-34
lines changed
Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,67 @@
1+
## Leetcode Blog Version
12

2-
## TODO
3-
* write down thinking
3+
Here is the [Leetcode Blog Version](http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html).
4+
This is a classic problem. I write my first leetcode accepted version based on that.
45

6+
7+
## My own words version
8+
9+
Brute force + isPalindromic will run out of time.
10+
11+
### Recursive
12+
13+
* `length 1 substring`: all are palindromic
14+
* `length 2 substring`: only if two char are same
15+
16+
What if `length 3`
17+
18+
```
19+
20+
length 3 = length 1 + two char
21+
22+
0 1 2 3 4
23+
a b c b a
24+
+ ^
25+
| |
26+
+---+
27+
28+
29+
0 1 2 4 4
30+
a b c b a
31+
+ ^
32+
| |
33+
+---+
34+
35+
```
36+
37+
* `length n` = inner `length n - 2` is palindromic AND (first char == last char)
38+
39+
40+
### Store `length n` into `P[lenght n][start index]`
41+
42+
* `P[1][3] = true` means that the substring starts from index 3 and 1 char long is palindromic.
43+
* `P[5][2] = true` means that the substring starts from index 2 and 5 char long is NOT palindromic.
44+
45+
46+
a matrix for `abcba`
47+
48+
```
49+
50+
0 1 2 3 4
51+
a b c b a
52+
1 1 1 1 1 1
53+
2 0 0 0 0 -
54+
3 0 1 0 - -
55+
4 0 0 - - -
56+
5 1 - - - -
57+
^
58+
l
59+
e
60+
n
61+
62+
```
63+
64+
With the matrix, we can fill up the martix by `P[len][i] = P[len -2][i + 1] && S[i] == S[i + len -1]`.
65+
66+
67+
Note: the `length 0` is useless, so just using `P[len - 1]` for `len`.
Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
public class Solution {
22
public String longestPalindrome(String s) {
33

4+
char[] S = s.toCharArray();
45

5-
// http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html
6+
if(S.length == 0) return "";
67

7-
final char[] S = s.toCharArray();
8-
final int N = S.length;
8+
boolean[][] P = new boolean[S.length][S.length];
99

10-
if(N == 0) return "";
11-
if(N == 1) return s;
12-
if(N == 2) return S[0] == S[1] ? s : "";
10+
int maxi = 0;
11+
int maxlen = 1;
1312

14-
boolean[][] P = new boolean[N][N];
13+
// len 1
14+
Arrays.fill(P[1 - 1], true);
1515

16-
for(int i = 0; i < N - 1; i++){
17-
P[i][i] = true;
18-
P[i][i + 1] = S[i] == S[i + 1];
19-
}
20-
21-
P[N - 1][N - 1] = true;
22-
23-
for(int j = 2; j < N; j++){
24-
for(int i = 0; i < j; i++){
25-
P[i][j] |= P[i + 1][j - 1] && S[i] == S[j];
16+
// len 2
17+
for(int i = 0; i < S.length - 1; i++){
18+
if(S[i] == S[i + 2 - 1]){
19+
P[2 - 1][i] = true;
20+
maxi = i;
21+
maxlen = 2;
2622
}
2723
}
2824

29-
int mi = 0;
30-
int mj = 0;
31-
32-
for(int i = 0; i < N; i++){
33-
for(int j = 0; j < N; j++){
34-
if(P[i][j] && (j - i > mj - mi)){
35-
mi = i;
36-
mj = j;
25+
// len 3 to max
26+
for(int len = 3; len <= S.length; len++){
27+
28+
for(int i = 0; i < S.length - (len - 1); i++){
29+
30+
if(P[len - 1 - 2][i + 1] && S[i] == S[i + len - 1]){
31+
P[len - 1][i] = true;
32+
maxi = i;
33+
maxlen = len;
3734
}
3835
}
39-
}
36+
}
4037

41-
return new String(S, mi, mj - mi + 1);
38+
return s.substring(maxi, maxi + maxlen);
4239
}
43-
}
40+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
## Recursion
12

2-
## TODO
3-
* write down thinking
3+
```
4+
i = 0 .. end
5+
6+
partition(s) = s[0..i] + partition(s[i+1 .. end]) if s[0..i] is palindromic
7+
8+
```
49

longest-palindromic-substring/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: Longest Palindromic Substring
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-18 17:56:19 +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/' }} %}

palindrome-partitioning/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
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-18 23:49:32 +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)