From d6e97328c205bf3cd130fb3a148931c9a29b4c51 Mon Sep 17 00:00:00 2001 From: WayneCui Date: Sun, 17 Feb 2019 20:12:07 +0800 Subject: [PATCH] add KMP algorithm --- go/34_kmp/kmp.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 go/34_kmp/kmp.go diff --git a/go/34_kmp/kmp.go b/go/34_kmp/kmp.go new file mode 100644 index 00000000..75941f16 --- /dev/null +++ b/go/34_kmp/kmp.go @@ -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 +} \ No newline at end of file