Skip to content

Commit

Permalink
1142 Longest Common Subsequence golang solution:
Browse files Browse the repository at this point in the history
  • Loading branch information
Андрей Федоров authored and Андрей Федоров committed Dec 15, 2022
1 parent 2c65a33 commit bcff6ca
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions go/1143-Longest-Common-Subsequence.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit bcff6ca

Please sign in to comment.