add new solutions
This commit is contained in:
parent
886b5e0a8e
commit
67cad91898
47 changed files with 1549 additions and 1 deletions
24
solutions/11/q1143/solution.go
Normal file
24
solutions/11/q1143/solution.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package q1143
|
||||
|
||||
// Note: Can be compressed to only len(text1) extra space.
|
||||
|
||||
func longestCommonSubsequence(text1 string, text2 string) int {
|
||||
commonSeqLen := make([][]int, len(text1)+1)
|
||||
for i := range commonSeqLen {
|
||||
commonSeqLen[i] = make([]int, len(text2)+1)
|
||||
}
|
||||
|
||||
for i1 := 1; i1 <= len(text1); i1++ {
|
||||
for i2 := 1; i2 <= len(text2); i2++ {
|
||||
if text1[i1-1] == text2[i2-1] {
|
||||
commonSeqLen[i1][i2] = commonSeqLen[i1-1][i2-1] + 1
|
||||
} else {
|
||||
commonSeqLen[i1][i2] = max(commonSeqLen[i1-1][i2], commonSeqLen[i1][i2-1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return commonSeqLen[len(text1)][len(text2)]
|
||||
}
|
||||
|
||||
var _ = longestCommonSubsequence
|
||||
Loading…
Add table
Add a link
Reference in a new issue