-
Notifications
You must be signed in to change notification settings - Fork 0
/
1143.longest-common-subsequence.go
60 lines (52 loc) · 1.05 KB
/
1143.longest-common-subsequence.go
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
/*
* @lc app=leetcode id=1143 lang=golang
*
* [1143] Longest Common Subsequence
*/
// @lc code=start
func longestCommonSubsequence(text1 string, text2 string) int {
// create a cache
cache := make([][]int, len(text1)+1)
for i := 0; i < len(text1)+1; i++ {
cache[i] = make([]int, len(text2)+1)
}
/*
text1 = "abcde", text2 = "ace"
cache:
"" a b c d e
0 0 0 0 0 0 0
a 0 0 0 0 0 0
c 0 0 0 0 0 0
e 0 0 0 0 0 0
*/
// DP (iteration + caching)
for i := 1; i < len(text1)+1; i++ {
for j := 1; j < len(text2)+1; j++ {
if text1[i-1] == text2[j-1] {
cache[i][j] = cache[i-1][j-1] + 1
} else {
cache[i][j] = max(cache[i-1][j], cache[i][j-1])
}
}
}
/*
text1 = "abcde", text2 = "ace"
cache:
"" a b c d e
0 0 0 0 0 0 0
a 0 1 1 1 1 1
c 0 1 1 2 2 2
e 0 1 1 2 2 3
*/
// Using DP (tabulation)
// if len(text1) = M, len(text2) = N
// TC: O(M * N), SC: O(M * N)
return cache[len(text1)][len(text2)]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
// @lc code=end=