Skip to content

Commit 0845069

Browse files
committed
Implement unit test for boyer moore
1 parent 3aee148 commit 0845069

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
3+
namespace Algorithms.Strings.Search
4+
{
5+
public class BoyerMoore
6+
{
7+
private int[] right;
8+
private const int R = 256;
9+
private int M;
10+
private string pat;
11+
public BoyerMoore(string pat)
12+
{
13+
this.pat = pat;
14+
right = new int[R];
15+
for (int i = 0; i < pat.Length; ++i)
16+
{
17+
right[pat[i]] = i;
18+
}
19+
M = pat.Length;
20+
}
21+
22+
public int Search(string text)
23+
{
24+
int skip = 1;
25+
for (int i = 0; i < text.Length - M; i+=skip)
26+
{
27+
var j;
28+
for (j = M - 1; j >= 0; j--)
29+
{
30+
if (text[i + j] != pat[j])
31+
{
32+
skip = Math.Max(1, j - right[text[i + j]]);
33+
break;
34+
}
35+
}
36+
if (j == -1) return i;
37+
}
38+
return -1;
39+
}
40+
41+
}
42+
}

0 commit comments

Comments
 (0)