This is a KMP(Knuth–Morris–Pratt algorithm) implement and related string function Strstr
and Strchr
which using KMP
feature inside.
In computer science, the Knuth–Morris–Pratt string searching algorithm (or KMP algorithm) searches for occurrences of a "word" W within a main "text string" S by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin, thus bypassing re-examination of previously matched characters.
A chinese version How KMP work note is here
go get github.com/kkdai/kmp
package main
import (
"fmt"
"github.com/kkdai/kmp"
)
func main() {
//Using KMP
fmt.Println(KMP("co", "cocacola"))
//0, 4
//Using Strstr
fmt.Println(Strstr("cocacola", "co"))
//0
//Using Strchr
fmt.Println(Strstr("cocacola", "co"))
//4
}
- Knuth-Morris-Pratt algorithm
- 理解KMP算法
- String Matching: 各種演算法介紹
- youtube: Tutorial: The Knuth-Morris-Pratt (KMP) String Matching Algorithm
- youtube: Knuth–Morris–Pratt(KMP) Pattern Matching(Substring search)
- Regular Expression Matching with a Trigram Index
It is one of my project 52.
This package is licensed under MIT license. See LICENSE for details.