Skip to content

Commit

Permalink
feat: adding testing for kmp algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
rulanugrh committed Mar 12, 2024
1 parent 08e8c5e commit 2896eb7
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
16 changes: 16 additions & 0 deletions algorithm/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ func TestCompressLZW(t *testing.T) {

assert.Equal(t, expected, str)
}

func TestKMP(t *testing.T) {
str := SearchString("syahara", "sy")

assert.Equal(t, 0, str)
}

func TestKMPLast(t *testing.T) {
str := SerchNext("syahara", "ha")
assert.Equal(t, 3, str)
}

func TestKMPNone(t *testing.T) {
str := SearchString("araa", "rul")
assert.Equal(t, -1, str)
}
105 changes: 105 additions & 0 deletions algorithm/kmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package algorithm


const (
PatternSize int = 100
)

func SerchNext(stack string, needle string) int {
ret := KMP(stack, needle)
if len(ret) > 0 {
return ret[len(ret)-1]
}

return -1
}

func SearchString(stack string, needle string) int {

ret := KMP(stack, needle)
if len(ret) > 0 {
return ret[0]
}

return -1
}

func KMP(stack string, needle string) []int {
next := preKMP(needle)
i := 0
j := 0
m := len(needle)
n := len(stack)

x := []byte(needle)
y := []byte(stack)
var ret []int

if m == 0 || n == 0 {
return ret
}

if n < m {
return ret
}

for j < n {
for i > -1 && x[i] != y[j] {
i = next[i]
}
i++
j++

if i >= m {
ret = append(ret, j-i)
i = next[i]
}
}

return ret
}
func preMP(x string) [PatternSize]int {
var i, j int
lenght := len(x) - 1

var mpNext [PatternSize]int
i = 0
j = -1
mpNext[0] = -1

for i < lenght {
for j > -1 && x[i] != x[j] {
j = mpNext[j]
}
i++
j++
mpNext[i] = j
}

return mpNext
}

func preKMP(x string) [PatternSize]int {
var i, j int
lenght := len(x) - 1

var kmpNext [PatternSize]int
i = 0
j = -1
kmpNext[0] = -1

for i < lenght {
for j > -1 && x[i] != x[j] {
j = kmpNext[j]
}
i++
j++

if x[i] == x[j] {
kmpNext[i] = kmpNext[j]
} else {
kmpNext[i] = j
}
}
return kmpNext
}

0 comments on commit 2896eb7

Please sign in to comment.