Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leonardpepa/string-matching-go
String matching algorithms written in go
https://github.com/leonardpepa/string-matching-go
algorithms brute-force dfa go golang introduction-to-algorithms knuth-morris-pratt rabin-karp-algorithm string-matching
Last synced: 27 days ago
JSON representation
String matching algorithms written in go
- Host: GitHub
- URL: https://github.com/leonardpepa/string-matching-go
- Owner: Leonardpepa
- Created: 2024-01-03T03:32:17.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-01-07T21:32:11.000Z (10 months ago)
- Last Synced: 2024-01-24T12:07:02.940Z (10 months ago)
- Topics: algorithms, brute-force, dfa, go, golang, introduction-to-algorithms, knuth-morris-pratt, rabin-karp-algorithm, string-matching
- Language: Go
- Homepage: https://github.com/Leonardpepa/string-matching-go?tab=readme-ov-file
- Size: 1.68 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# String matching algorithms written in go
## Purpose
* Educational
## Description
This project is an implementation of various string matching algorithms described
in [Introduction to algorithms](https://dl.ebooksworld.ir/books/Introduction.to.Algorithms.4th.Leiserson.Stein.Rivest.Cormen.MIT.Press.9780262046305.EBooksWorld.ir.pdf)## Algorithms Implemented
* Brute force
* Rabin Karp
* DFA (Deterministic finite automaton)
* Knuth Morris Pratt## Time Complexity
* n = length of the input text
* m = length of the pattern
* This table shows the worst case time
Algorithms
Preprocessing time
Matching time
Brute Force
0
Ο((n - m + 1)m)
Rabin Karp
Θ(m)
Ο((n - m + 1)m)
DFA
Ο(m |Σ|)
Θ(n)
Knuth Morris Pratt
Θ(m)
Θ(n)
### String Matching Definition
The problem of finding occurrence(s) of a pattern string within another string or body of text.
The algorithm returns an array of the first index for every occurrence in the text## Example
```go
package mainimport (
"fmt"
"log"
"string-matching/internal/KMP"
)func main() {
input := `Project Gutenberg's International Short Stories: French, by VariousThis eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever. You may copy it, give it away or
re-use it under the terms of the Project Gutenberg License included
with this eBook or online at www.gutenberg.net
`pattern := "Gutenberg"
indexes, err := KMP.MatchString(input, pattern)
if err != nil {
log.Fatal(err)
}printMatches(indexes, input, len(pattern))
}func printMatches(indexes []int, input string, patternLen int) {
for _, index := range indexes {
fmt.Printf("found at %d, %s\n", index, input[index:index+patternLen])
}
}
```### output
```terminal
found at 8, Gutenberg
found at 244, Gutenberg
```# How to run
1. Clone the repo ```git clone https://github.com/Leonardpepa/string-matching-go.git```
2. Build ```go build```
3. run on windows```string-matching.exe```
4. run on linux ```./string-matching```
5. run tests ```go test```