From bcff6cace8857f9dcdbf95aa58f3e4dc7d94b965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=BE=D0=B2?= Date: Wed, 14 Dec 2022 21:21:47 -0800 Subject: [PATCH] 1142 Longest Common Subsequence golang solution: --- go/1143-Longest-Common-Subsequence.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 go/1143-Longest-Common-Subsequence.go diff --git a/go/1143-Longest-Common-Subsequence.go b/go/1143-Longest-Common-Subsequence.go new file mode 100644 index 000000000..286ea9935 --- /dev/null +++ b/go/1143-Longest-Common-Subsequence.go @@ -0,0 +1,26 @@ +func longestCommonSubsequence(text1 string, text2 string) int { + dp := make([][]int, len(text1) + 1) + + for i := 0; i < len(dp); i++ { + dp[i] = make([]int, len(text2) + 1) + } + + for i := len(text1) - 1; i >= 0; i-- { + for j := len(text2) - 1; j >= 0; j-- { + if text1[i] == text2[j] { + dp[i][j] = 1 + dp[i + 1][j + 1] + } else { + dp[i][j] = max(dp[i][j + 1], dp[i + 1][j]) + } + } + } + return dp[0][0] +} + +func max(a, b int) int { + if a > b { + return a + } + + return b +}