Skip to content

Commit f54888f

Browse files
authored
Merge pull request #1549 from razer96/1143-Longest-Common-Subsequence.go
1143 Longest Common Subsequence golang solution
2 parents 456f323 + bcff6ca commit f54888f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

go/1143-Longest-Common-Subsequence.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func longestCommonSubsequence(text1 string, text2 string) int {
2+
dp := make([][]int, len(text1) + 1)
3+
4+
for i := 0; i < len(dp); i++ {
5+
dp[i] = make([]int, len(text2) + 1)
6+
}
7+
8+
for i := len(text1) - 1; i >= 0; i-- {
9+
for j := len(text2) - 1; j >= 0; j-- {
10+
if text1[i] == text2[j] {
11+
dp[i][j] = 1 + dp[i + 1][j + 1]
12+
} else {
13+
dp[i][j] = max(dp[i][j + 1], dp[i + 1][j])
14+
}
15+
}
16+
}
17+
return dp[0][0]
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
25+
return b
26+
}

0 commit comments

Comments
 (0)