Java Program to Implement Boyer Moore Algorithm

This is a Java Program to Implement Boyer Moore Algorithm. The Boyer–Moore string search algorithm is an efficient string searching algorithm. The algorithm preprocesses the string being searched for (the pattern), but not the string being searched in (the text). It is thus well-suited for applications in which the pattern either is much shorter than the text or does persist across multiple searches. The Boyer-Moore algorithm uses information gathered during the preprocess step to skip sections of the text, resulting in a lower constant factor than many other string algorithms. In general, the algorithm runs faster as the pattern length increases.

Here is the source code of the Java Program to Implement Boyer Moore Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  ** Java Program to implement Boyer Moore Algorithm
  3.  **/
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. import java.io.IOException;
  8.  
  9. /** Class BoyerMoore **/
  10. public class BoyerMoore
  11. {
  12.     /** function findPattern **/
  13.     public void findPattern(String t, String p)
  14.     {
  15.         char[] text = t.toCharArray();
  16.         char[] pattern = p.toCharArray();
  17.         int pos = indexOf(text, pattern);
  18.         if (pos == -1)
  19.             System.out.println("\nNo Match\n");
  20.         else
  21.             System.out.println("Pattern found at position : "+ pos);
  22.     }
  23.     /** Function to calculate index of pattern substring **/
  24.     public int indexOf(char[] text, char[] pattern) 
  25.     {
  26.         if (pattern.length == 0) 
  27.             return 0;
  28.         int charTable[] = makeCharTable(pattern);
  29.         int offsetTable[] = makeOffsetTable(pattern);
  30.         for (int i = pattern.length - 1, j; i < text.length;) 
  31.         {
  32.             for (j = pattern.length - 1; pattern[j] == text[i]; --i, --j) 
  33.                      if (j == 0) 
  34.                     return i;
  35.  
  36.               // i += pattern.length - j; // For naive method
  37.               i += Math.max(offsetTable[pattern.length - 1 - j], charTable[text[i]]);
  38.         }
  39.         return -1;
  40.       }
  41.       /** Makes the jump table based on the mismatched character information **/
  42.       private int[] makeCharTable(char[] pattern) 
  43.       {
  44.         final int ALPHABET_SIZE = 256;
  45.         int[] table = new int[ALPHABET_SIZE];
  46.         for (int i = 0; i < table.length; ++i) 
  47.                table[i] = pattern.length;
  48.         for (int i = 0; i < pattern.length - 1; ++i) 
  49.                table[pattern[i]] = pattern.length - 1 - i;
  50.         return table;
  51.       }
  52.       /** Makes the jump table based on the scan offset which mismatch occurs. **/
  53.       private static int[] makeOffsetTable(char[] pattern) 
  54.       {
  55.         int[] table = new int[pattern.length];
  56.         int lastPrefixPosition = pattern.length;
  57.         for (int i = pattern.length - 1; i >= 0; --i) 
  58.         {
  59.             if (isPrefix(pattern, i + 1)) 
  60.                    lastPrefixPosition = i + 1;
  61.               table[pattern.length - 1 - i] = lastPrefixPosition - i + pattern.length - 1;
  62.         }
  63.         for (int i = 0; i < pattern.length - 1; ++i) 
  64.         {
  65.               int slen = suffixLength(pattern, i);
  66.               table[slen] = pattern.length - 1 - i + slen;
  67.         }
  68.         return table;
  69.     }
  70.     /** function to check if needle[p:end] a prefix of pattern **/
  71.     private static boolean isPrefix(char[] pattern, int p) 
  72.     {
  73.         for (int i = p, j = 0; i < pattern.length; ++i, ++j) 
  74.             if (pattern[i] != pattern[j]) 
  75.                   return false;
  76.         return true;
  77.     }
  78.     /** function to returns the maximum length of the substring ends at p and is a suffix **/
  79.     private static int suffixLength(char[] pattern, int p) 
  80.     {
  81.         int len = 0;
  82.         for (int i = p, j = pattern.length - 1; i >= 0 && pattern[i] == pattern[j]; --i, --j) 
  83.                len += 1;
  84.         return len;
  85.     }
  86.     /** Main Function **/
  87.     public static void main(String[] args) throws IOException
  88.     {    
  89.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  90.         System.out.println("Boyer Moore Algorithm Test\n");
  91.         System.out.println("\nEnter Text\n");
  92.         String text = br.readLine();
  93.         System.out.println("\nEnter Pattern\n");
  94.         String pattern = br.readLine();
  95.         BoyerMoore bm = new BoyerMoore(); 
  96.         bm.findPattern(text, pattern);     
  97.     }
  98. }

Boyer Moore Algorithm Test
 
 
Enter Text
 
abcdefghijklmnopqrstuvwxyz
 
Enter Pattern
 
qrstuv
 
Pattern found at position : 16

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.