forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-common-subsequence.cpp
37 lines (32 loc) · 1.16 KB
/
longest-common-subsequence.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
// Time: O(m * n)
// Space: O(min(m, n))
class Solution {
public:
/**
* @param A, B: Two strings.
* @return: The length of longest common subsequence of A and B.
*/
int longestCommonSubsequence(string A, string B) {
if (A.length() < B.length()) {
return longestCommonSubsequence(B, A);
}
// table[i][j] means the longest length of common subsequence
// of A[0 : i] and B[0 : j].
vector<vector<int>> table(2, vector<int>(A.length() + 1, 0));
// if A[i - 1] != B[j - 1]:
// table[i][j] = max(table[i - 1][j], table[i][j - 1])
// if A[i - 1] == B[j - 1]:
// table[i][j] = table[i - 1][j - 1] + 1
for (int i = 1; i < A.length() + 1; ++i) {
for (int j = 1; j < B.length() + 1; ++j) {
if (A[i - 1] != B[j - 1]) {
table[i % 2][j] = max(table[(i - 1) % 2][j],
table[i % 2][j - 1]);
} else {
table[i % 2][j] = table[(i - 1) % 2][j - 1] + 1;
}
}
}
return table[A.length() % 2][B.length()];
}
};