/kmp

KMP (Knuth–Morris–Pratt algorithm) implement and related string function `Strstr` and `Strchr` in Golang

Primary LanguageGoMIT LicenseMIT

KMP

GitHub license GoDoc Build Status

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

Install

go get github.com/kkdai/kmp

Usage

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
}	

Inspired

Project52

It is one of my project 52.

License

This package is licensed under MIT license. See LICENSE for details.