MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Naive

Function Naive

strings/search/naive.go:5–21  ·  view source on GitHub ↗

Implementation of naive string search O(n*m) where n=len(txt) and m=len(pattern)

(text string, pattern string)

Source from the content-addressed store, hash-verified

3// Implementation of naive string search
4// O(n*m) where n=len(txt) and m=len(pattern)
5func Naive(text string, pattern string) []int {
6 var positions []int
7 for i := 0; i <= len(text)-len(pattern); i++ {
8 var match bool = true
9 for j := 0; j < len(pattern); j++ {
10 if text[i+j] != pattern[j] {
11 match = false
12 break
13 }
14
15 }
16 if match {
17 positions = append(positions, i)
18 }
19 }
20 return positions
21}

Callers 1

TestNaiveFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestNaiveFunction · 0.68