Repository navigation
Expand file tree
/
Copy pathBoyerMoore.java
More file actions
56 lines (46 loc) · 1.54 KB
/
Copy pathBoyerMoore.java
File metadata and controls
56 lines (46 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.thealgorithms.searches;
import java.util.HashMap;
import java.util.Map;
/**
* Boyer-Moore string search algorithm.
* Efficient algorithm for substring search.
* https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-search_algorithm
*/
public class BoyerMoore {
private final Map<Character, Integer> right; // Bad character rule table: last index of each pattern character
private final String pattern;
public BoyerMoore(String pat) {
this.pattern = pat;
// A map instead of a 256-entry array, so characters outside extended ASCII work too
this.right = new HashMap<>();
for (int j = 0; j < pat.length(); j++) {
right.put(pat.charAt(j), j);
}
}
public int search(String text) {
if (pattern.isEmpty()) {
return 0;
}
int m = pattern.length();
int n = text.length();
int skip;
for (int i = 0; i <= n - m; i += skip) {
skip = 0;
for (int j = m - 1; j >= 0; j--) {
char txtChar = text.charAt(i + j);
char patChar = pattern.charAt(j);
if (patChar != txtChar) {
skip = Math.max(1, j - right.getOrDefault(txtChar, -1));
break;
}
}
if (skip == 0) {
return i; // Match found
}
}
return -1; // No match
}
public static int staticSearch(String text, String pattern) {
return new BoyerMoore(pattern).search(text);
}
}