This Java program implements the Boyer-Moore string matching algorithm using the Bad Character Heuristic. The Boyer-Moore algorithm is an efficient string-searching algorithm that skips sections of the text to reduce the number of comparisons needed.
-
Preprocessing the Pattern 🛠️
- The
badCharHeuristicfunction creates a table (badchar[]) that records the last occurrence of each character in the pattern. - If a character in the text does not match the corresponding character in the pattern, the algorithm uses this table to determine how far to shift the pattern.
- The
-
Searching the Text 🔎
- The
searchfunction slides the pattern over the text from left to right. - For each position, it compares the characters of the pattern with the corresponding characters in the text from right to left.
- If a mismatch occurs, it uses the
badchartable to determine the next shift. - If the pattern matches the current position in the text, it prints the position and continues to search for other occurrences.
- The
-
Output 📊
- The program prints the positions where the pattern is found in the text.
- If the pattern is not found, it prints a message indicating that the pattern is not in the text.
The implementation consists of several key components:
NO_OF_CHARS: A constant defining the number of possible characters (256)max(): A utility function to get the maximum of two integersbadCharHeuristic(): Preprocesses the pattern to create the bad character tablesearch(): The main searching function implementing the Boyer-Moore algorithmmain(): The driver program with a sample test case
-
Compile the program using a Java compiler:
javac BoyerMoore.java
-
Run the compiled program:
java BoyerMoore
For the input text "Test. This is a test." and pattern "test", the output will be:
Pattern found at position: 0
Pattern found at position: 15
If you change the pattern to a string not present in the text, for example, "xyz", the output will be:
Pattern not found in the text.
The Boyer-Moore algorithm is known for its efficiency:
- Best case: O(n/m) where n is the text length and m is the pattern length
- Worst case: O(n*m)
- Average case: O(n)
Feel free to contribute to this project by:
- Reporting issues 🐛
- Suggesting improvements 💡
- Submitting pull requests 🔄
This project is open source and available under the MIT License.