Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Longest-common-substring/Go/longest-common-substring.go
Normal file
28
Task/Longest-common-substring/Go/longest-common-substring.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func lcs(a, b string) (output string) {
|
||||
lengths := make([]int, len(a)*len(b))
|
||||
greatestLength := 0
|
||||
for i, x := range a {
|
||||
for j, y := range b {
|
||||
if x == y {
|
||||
if i == 0 || j == 0 {
|
||||
lengths[i*len(b)+j] = 1
|
||||
} else {
|
||||
lengths[i*len(b)+j] = lengths[(i-1)*len(b)+j-1] + 1
|
||||
}
|
||||
if lengths[i*len(b)+j] > greatestLength {
|
||||
greatestLength = lengths[i*len(b)+j]
|
||||
output = a[i-greatestLength+1 : i+1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(lcs("thisisatest", "testing123testing"))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue