Skip to content

Instantly share code, notes, and snippets.

@kehtolaulu
Created May 19, 2018 17:48
Show Gist options
  • Select an option

  • Save kehtolaulu/35dc48dfd8a51064282c9cc27b387fd5 to your computer and use it in GitHub Desktop.

Select an option

Save kehtolaulu/35dc48dfd8a51064282c9cc27b387fd5 to your computer and use it in GitHub Desktop.
Boyer-Moore algorithm
import java.util.HashMap;
public class BM {
public static void main(String[] args) {
String source = "abcde";
String template = "cd";
System.out.println(getFirstEntry(source, template));
}
public static HashMap<Character, Integer> makeOffsetTable(String pattern) {
HashMap<Character, Integer> offsetTable = new HashMap<Character, Integer>();
for (int i = 0; i <= 255; i++) {
offsetTable.put((char) i, pattern.length());
}
for (int i = 0; i < pattern.length() - 1; i++) {
offsetTable.put(pattern.charAt(i), pattern.length() - i - 1);
}
return offsetTable;
}
public static int getFirstEntry(String s, String p) {
HashMap<Character, Integer> offsetTable = makeOffsetTable(p);
if (s.length() < p.length()) {
return -1;
}
int i = p.length() - 1;
int j = i;
int k = i;
while (j >= 0 && i <= s.length() - 1) {
j = p.length() - 1;
k = i;
while (j >= 0 && s.charAt(k) == p.charAt(j)) {
k--;
j--;
}
i += offsetTable.get(s.charAt(i));
}
if (k >= s.length() - p.length()) {
return -1;
} else {
return k + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment