Implementation of naive string search O(n*m) where n=len(txt) and m=len(pattern)
(text string, pattern string)
| 3 | // Implementation of naive string search |
| 4 | // O(n*m) where n=len(txt) and m=len(pattern) |
| 5 | func 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 | } |