The Boyer-Moore AlgorithmThe Brute Force Algorithm finds all the possible substrings that can be matched with the search pattern, and searches all the characters of the given text. The Boyer-Moore (B-M) Algorithm is a faster algorithm when the search string is large. It is one of the best suitable algorithms for searching the words in the text document. Unlike the Brute Force algorithm, the Boyer-Moore Algorithm skips the unnecessary checks.
The B-M Algorithm works with a ‘backward’ approach, the target string is aligned with the start of the check string, and the last character of the target string is checked against the corresponding character in the check string. In the case of a match, then the second-to-last character of the target string is compared to the corresponding check string character. In the case of a mismatch, the algorithm computes a new alignment for the target string based on the mismatch. In the case of mismatch the algorithms skip checking some of the characters.
Boyer-Moore Algorithm steps: Character comparison is done from right of the pattern to the left:
- Constructing the SHIFT Table
- GOOD SUFFIX SHIFT or BAD CHARACTER SHIFT