Skip to content

Commit

Permalink
Merge pull request wangzheng0822#245 from WayneCui/master
Browse files Browse the repository at this point in the history
add KMP algorithm
  • Loading branch information
wangzheng0822 authored Feb 18, 2019
2 parents 14d2f25 + d6e9732 commit 4dc94cf
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions go/34_kmp/kmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"fmt"
)

func getNexts(pattern string) []int {
m := len(pattern)
nexts := make([]int, m)
for index := range nexts {
nexts[index] = -1
}

for i := 1; i < m - 1; i++ {
j := nexts[i - 1]

for pattern[j + 1] != pattern[i] && j >= 0 {
j = nexts[j]
}

if pattern[j + 1] == pattern[i] {
j += 1
}

nexts[i] = j
}

return nexts
}

func findByKMP(s string, pattern string) int {
n := len(s)
m := len(pattern)
if n < m {
return -1
}

nexts := getNexts(pattern)

j := 0
for i := 0; i < n; i++ {
for j > 0 && s[i] != pattern[j] {
j = nexts[j - 1] + 1
}

if s[i] == pattern[j] {
if j == m - 1 {
return i - m + 1
}
j += 1
}
}

return -1
}

func main(){
s := "abc abcdab abcdabcdabde"
pattern := "bcdabd"
fmt.Println(findByKMP(s, pattern)) //16

s = "aabbbbaaabbababbabbbabaaabb"
pattern = "abab"
fmt.Println(findByKMP(s, pattern)) //11

s = "aabbbbaaabbababbabbbabaaabb"
pattern = "ababacd"
fmt.Println(findByKMP(s, pattern)) //-1

s = "hello"
pattern = "ll"
fmt.Println(findByKMP(s, pattern)) //2
}

0 comments on commit 4dc94cf

Please sign in to comment.