We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 456f323 + bcff6ca commit f54888fCopy full SHA for f54888f
go/1143-Longest-Common-Subsequence.go
@@ -0,0 +1,26 @@
1
+func longestCommonSubsequence(text1 string, text2 string) int {
2
+ dp := make([][]int, len(text1) + 1)
3
+
4
+ for i := 0; i < len(dp); i++ {
5
+ dp[i] = make([]int, len(text2) + 1)
6
+ }
7
8
+ for i := len(text1) - 1; i >= 0; i-- {
9
+ for j := len(text2) - 1; j >= 0; j-- {
10
+ if text1[i] == text2[j] {
11
+ dp[i][j] = 1 + dp[i + 1][j + 1]
12
+ } else {
13
+ dp[i][j] = max(dp[i][j + 1], dp[i + 1][j])
14
15
16
17
+ return dp[0][0]
18
+}
19
20
+func max(a, b int) int {
21
+ if a > b {
22
+ return a
23
24
25
+ return b
26
0 commit comments