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")) }