Skip to content

Commit

Permalink
feat: 1143. Longest Common Subsequence (#50)
Browse files Browse the repository at this point in the history
Signed-off-by: ashing <axingfly@gmail.com>
  • Loading branch information
ronething authored Jan 25, 2024
1 parent 270f4e4 commit 9236ab2
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions leetcode/1143/1143. Longest Common Subsequence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package _143

func longestCommonSubsequence(text1 string, text2 string) int {
m, n := len(text1), len(text2)
dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
}

for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
if text1[i-1] == text2[j-1] {
dp[i][j] = dp[i-1][j-1] + 1
} else {
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
}
}
}

return dp[m][n]
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

0 comments on commit 9236ab2

Please sign in to comment.