-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path79.longest-common-substring.cpp
73 lines (68 loc) · 1.5 KB
/
79.longest-common-substring.cpp
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
// Tag: Dynamic Programming/DP, Two Sequences DP
// Time: O(NM)
// Space: O(NM)
// Ref: -
// Note: -
// Given two strings, find the longest common substring.
//
// Return the length of it.
//
// **Example 1:**
//
// Input:
// ```
// stringA = "ABCD"
// stringB = "CBCE"
// ```
// Output:
// ```
// 2
// ```
// Explanation:
//
// Longest common substring is "BC"
//
// **Example 2:**
//
// Input:
// ```
// stringA = "ABCD"
// stringB = "EACB"
// ```
// Output:
// ```
// 1
// ```
// Explanation:
//
// Longest common substring is 'A' or 'C' or 'B'
//
// The characters in **substring** should occur continuously in original string. This is different with **subsequence**.
class Solution {
public:
/**
* @param a: A string
* @param b: A string
* @return: the length of the longest common substring.
*/
int longestCommonSubstring(string &a, string &b) {
// write your code here
int n = a.size();
int m = b.size();
vector<vector<int>> dp(n, vector<int>(m, 0));
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
if (i > 0 && j > 0) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = 1;
}
res = max(res, dp[i][j]);
}
}
}
return res;
}
};