How does the Boyer Moore algorithm work?
How does the Boyer Moore algorithm work?
Coding Ninjas

Are you looking for an algorithm to solve your pattern-matching question? If yes, the Boyer Moore algorithm is all you need to know. 


The Boyer Moore Pattern Matching Algorithm is used by programmers due to the less time complexity and space complexity that it takes for searching the substring. 

Pattern Matching Algorithm is a most asked question on the coding platform as well as in the interviews. In the pattern-matching algorithm, you will have to search for a substring that will be given in the string. 


So, continue reading till the end to learn more about the concept of Boyer Moore algorithm.

What is the Boyer Moore Algorithm?

Boyer Moore Algorithm is used for pattern matching or string matching from a given string. It is considered one of the efficient algorithms that are being used in a lot of applications for searching from the string. 

The working of the Boyer Moore Algorithm is very simple. When any user calls the function, then it starts comparing each of the characters which are in the string and compares it with the given string to check whether there is any substring available in the string or not. Once the substrings are found, then the output is displayed on your screen according to your need. 

The Boyer Moore Algorithm is a combination of famous approaches which are Bad Character and Good Suffix Heuristics. Through both approaches, we get our output in a very efficient time which is good for our programming. 

Why should I learn the Boyer Moore Algorithm?

The Boyer Moore algorithm is used to search for a substring from the string. Due to this, the algorithm is also implemented for the search bar that we often see on different websites. The same algorithm is used there due to which we get to see the search results. 

When we type anything in the search bar, then it is taken as a substring in the backend and then it is compared with all the data that is available on their database. Once the processes are done, then we got our search results. So, you will only be able to implement it in the search bar if you are aware of its concept. 

Apart from it, some famous IT companies have also asked for the Boyer Moore algorithm in their interviews. So, if you want to get placed at some famous IT company, then you will have to understand its concept as well as how it works. By learning its concept, you will be able to solve the questions which are based on string compression, substrings, and much more. 

Working of Boyer Moore Algorithm

The Boyer Moore Algorithm is used for checking a particular substring from the string. For searching the substring, an iteration is used which checks for the given substring from the string. 

To illustrate its working, here is an example. 

Problem Statement

From the given string “ABAAABCD”, find “ABC”.



  • For checking whether the ABC exists in the given string, we will iterate over the string. 

  • We will be converting and storing the character with its binary number in every iteration. After storing, we will check the same with our given string binary number. 

  • If the binary number matches the given character’s binary number, then we will check the next character of both of the strings. 

  • Once we have got our all characters, then it's a match, otherwise, we will increase the starting index. 

  • Again we will iterate and check for the binary number. 

  • If we have found the substring and length have ended, then we will get out of the iteration with our all results. 


Dry Run

We are doing the dry run for the given example to make you more clear with the concept. 

  • First of all, we will take variables that will be storing the binary number of the characters. Similarly, we will also take another variable where we will store the variable of the given string which we have to find. 

  • Now, we will start iterating. 

  • For the first iteration, we will compare both strings. If both are equivalent, then we will store them. Here, in our first iteration, we will get A=A. So, our first element has been found. 

  • Now, we will do the second iteration, after iterating, we will get the element B=B. So, we have found out that our second element is equivalent. 

  • For the last element, we will check whether the element is equal or not. 

  • So, again the iteration will be done and the element will be converted. Now, we can see that the character that is in the string is A which is not equal to the character C in our given string. 

  • So, we will start iterating again by increasing the index. 

  • Again after iteration, we will increase the index as the character that is there is B. 

  • Now, again we will start the iteration. Once we have got our index at 4, then we are close to our result. 

  • So, we will start the iteration once again. We will check for the elements. 

  • You can see that all the elements will match the given substring, so we have got our result. 

  • However, the iteration will keep on running till the length gets ended of the string. 

  • So, we have finally got the answer. Now, the iteration will be finished. 

An important thing that you should remember, in different cases, there will be different substrings that will be available for the given string that we have to find. So, at that time, you will have to store the index for the output or count for finding the number of the same substrings. 


For getting placed at a good IT company, you need to have a good understanding of the DSA problems like string compression, Boyer Moore, Pattern Matching, and much more. 

Through all this, you will be able to ace your programming skills. So, keep on learning the data structure & algorithms for improving your problem-solving skills.