Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLongest Common Substring.java
More file actions
Latest commit
executable file
·179 lines (153 loc) · 4.51 KB
/
Copy pathLongest Common Substring.java
File metadata and controls
executable file
·179 lines (153 loc) · 4.51 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
M
1525239157
tags: DP, DoubleSequenceDP, String, SequenceDP
#### DoubleSequenceDP
- 两个string, 找最值: longestcommonstringlength
- 序列型, 并且是双序列, 找两个序列 (两维的某种性质)
- dp[i][j]: 对于A的前i个字母, 对于B的前j个字母, 找最长公共substring的长度
- dp = newint[m + 1][n + 1]
- dp[i][j] = dp[i - 1][j - 1] + 1; onlyifA.charAt(i - 1) == B.charAt(j - 1)
- 注意trackmax, 最后return
- spaceO(n^2), time(n^2)
##### Rollingarray
- 空间优化, [i] 只有和 [i - 1] 相关, 空间优化成O(n)
#### String
- 找所有A的substring, 然后B.contains()
- trackmaxsubstringlength
- O(n^2) time
```
/*
Given two strings, find the longest common substring.
Return the length of it.
Example
Given A = "ABCD", B = "CBCE", return 2.
Note
The characters in substring should occur continuously in original string. This is different with subsequence.
Challenge
O(n x m) time and memory.
Tags Expand
LintCode Copyright Longest Common Subsequence Dynamic Programming
*/
/**
Thoughts: double sequence DP
dp[i][j]: for first i chars in A, and first j chars in B, what's the longest common substring length?
init:
if i == 0 || j == 0, no common substring, dp[i][j] = 0;
function:
dp[i][j] = dp[i - 1][j - 1] + 1; only if A.charAt(i - 1) == B.charAt(j - 1)
otherwise, dp[i][j] = 0; start over
Track max
*/
publicclassSolution {
publicintlongestCommonSubstring(StringA, StringB) {
if (A == null || B == null || A.length() == 0 || B.length() == 0) {
return0;
}
intm = A.length();
intn = B.length();
int[][] dp = newint[m + 1][n + 1];
intmax = 0;
for (inti = 0; i <= m; i++) {
for(intj = 0; j <= n; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
continue;
}
if (A.charAt(i - 1) == B.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = 0;
}
max = Math.max(max, dp[i][j]);
}
}
returnmax;
}
}
// Rolling array, space O(n)
publicclassSolution {
publicintlongestCommonSubstring(StringA, StringB) {
if (A == null || B == null || A.length() == 0 || B.length() == 0) {
return0;
}
intm = A.length();
intn = B.length();
int[][] dp = newint[2][n + 1];
intmax = 0;
for (inti = 0; i <= m; i++) {
for(intj = 0; j <= n; j++) {
if (i == 0 || j == 0) {
dp[i % 2][j] = 0;
continue;
}
if (A.charAt(i - 1) == B.charAt(j - 1)) {
dp[i % 2][j] = dp[(i - 1) % 2][j - 1] + 1;
} else {
dp[i % 2][j] = 0;
}
max = Math.max(max, dp[i % 2][j]);
}
}
returnmax;
}
}
/**
String: find all possible substring, try all of them
O(n^2)
*/
publicclassSolution {
publicintlongestCommonSubstring(StringA, StringB) {
if (A == null || B == null || A.length() == 0 || B.length() == 0) {
return0;
}
intn = A.length();
intmax = 0;
for (inti = 0; i < n; i++) {
for(intj = 0; j <= i; j++) {
StringsubStr = A.substring(j, i + 1);
if (B.contains(subStr)) {
max = Math.max(max, subStr.length());
}
}
}
returnmax;
}
}
/**
Previous notes
Thoughts:
1. Compare all i X j.
2. Use a D[i][j] to mark the amount of common substring based on D[i - 1][j -1]. Could be 0.
3. track max length
NOTE1: create 2D array that's [N + 1][M + 1] because we want to hold D[n][M] in the 2d array
NOTE2: be carefule with init index 0's
*/
publicclassSolution {
/**
* @param A, B: Two string.
* @return: the length of the longest common substring.
*/
publicintlongestCommonSubstring(StringA, StringB) {
if (A == null || B == null || A.length() == 0 || B.length() == 0) {
return0;
}
int [][] D = newint[A.length() + 1][B.length() + 1];
intmax = 0;
for (inti = 0; i <= A.length(); i++) {
for(intj = 0; j <= B.length(); j++) {
if (i == 0 || j == 0) {
D[i][j] = 0;
} else {
if (A.charAt(i - 1) == B.charAt(j - 1)) {
D[i][j] = D[i - 1][j - 1] + 1;
} else {
D[i][j] = 0;
}
max = Math.max(max, D[i][j]);
}
}
}
returnmax;
}
}
```