aboutsummaryrefslogtreecommitdiffstats
path: root/1143/main.go
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-12-15 10:32:15 +0000
committerterminaldweller <thabogre@gmail.com>2022-12-15 10:32:15 +0000
commitcb80b1dcdb3c4822464e64a10768d21d473e3b78 (patch)
treeedaf816df752e08c464a636786b914f3ffa0a772 /1143/main.go
parent198 (diff)
downloadleetcode-cb80b1dcdb3c4822464e64a10768d21d473e3b78.tar.gz
leetcode-cb80b1dcdb3c4822464e64a10768d21d473e3b78.zip
1143
Diffstat (limited to '1143/main.go')
-rw-r--r--1143/main.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/1143/main.go b/1143/main.go
new file mode 100644
index 0000000..20b1bc0
--- /dev/null
+++ b/1143/main.go
@@ -0,0 +1,38 @@
+package main
+
+import "fmt"
+
+func max(a, b int) int {
+ if a > b {
+ return a
+ }
+ return b
+}
+
+func longestCommonSubsequence(text1 string, text2 string) int {
+ l1 := len(text1)
+ l2 := len(text2)
+ dp := make([][]int, l1+1)
+ for i := range dp {
+ dp[i] = make([]int, l2+1)
+ }
+
+ for i := 0; i < l1; i++ {
+ for j := 0; j < l2; j++ {
+ if text1[i] == text2[j] {
+ dp[i+1][j+1] = dp[i][j] + 1
+ } else {
+ dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])
+ }
+ }
+ }
+
+ return dp[l1][l2]
+}
+
+func main() {
+ fmt.Println(longestCommonSubsequence("abcde", "ace"))
+ fmt.Println(longestCommonSubsequence("abc", "abc"))
+ fmt.Println(longestCommonSubsequence("abc", "def"))
+ fmt.Println(longestCommonSubsequence("aced", "aceaced"))
+}