{{announcement.body}}
{{announcement.title}}

# Algorithm of the Week: Brute Force String Matching

DZone 's Guide to

# Algorithm of the Week: Brute Force String Matching

· Java Zone ·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

String matching is something crucial for database development and text processing software. Fortunately, every modern programming language and library is full of functions for string processing that help us in our everyday work. However it's important to understand their principles.

String algorithms can typically be divided into several categories. One of these categories is string matching.

When it comes to string matching, the most basic approach is what is known as brute force, which simply means to check every single character from the text to match against the pattern. In general we have a text and a pattern (most commonly shorter than the text). What we need to do is to answer the question whether this pattern appears in the text.

## Overview

The principles of brute force string matching are quite simple. We must check for a match between the first characters of the pattern with the first character of the text as on the picture bellow.

If they don’t match, we move forward to the second character of the text. Now we compare the first character of the pattern with the second character of the text. If they don’t match again, we move forward until we get a match or until we reach the end of the text.

In case they match, we move forward to the second character of the pattern comparing it with the “next” character of the text, as shown in the picture bellow.

Just because we have found a match between the first character from the pattern and some character of the text, doesn’t mean that the pattern appears in the text. We must move forward to see whether the full pattern is contained in the text.

## Implementation

Implementation of brute force string matching is easy and here we can see a short PHP example. The bad news is that this algorithm is naturally quite slow.

```function sub_string(\$pattern, \$subject)
{
\$n = strlen(\$subject);
\$m = strlen(\$pattern);

for (\$i = 0; i < \$n-\$m; \$i++) {
\$j = 0;
while (\$j < \$m && \$subject[\$i+\$j] == \$pattern[\$j]) {
\$j++;
}
if (\$j == \$m) return \$i;
}
return -1;
}

echo sub_string('o wo', 'hello world!');```

## Complexity

As I said this algorithm is slow. Actually every algorithm that contains “brute force” in its name is slow, but to show how slow string matching is, I can say that its complexity is O(n.m). Here n is the length of the text, while m is the length of the pattern.

In case we fix the length of the text and test against variable length of the pattern, again we get a rapidly growing function.

## Application

Brute force string matching can be very ineffective, but it can also be very handy in some cases. Just like the sequential search.

### It can be very useful…

1. Doesn’t require pre-processing of the text – Indeed if we search the text only once we don’t need to pre-process it. Most of the algorithms for string matching need to build an index of the text in order to search quickly. This is great when you’ve to search more than once into a text, but if you do only once, perhaps (for short texts) brute force matching is great!
2. Doesn’t require additional space – Because brute force matching doesn’t need pre-processing it also doesn’t require more space, which is one cool feature of this algorithm
3. Can be quite effective for short texts and patterns

### It can be ineffective…

1. If we search the text more than once – As I said in the previous section if you perform the search more than once it’s perhaps better to use another string matching algorithm that builds an index, and it’s faster.
2. It’s slow – In general brute force algorithms are slow and brute force matching isn’t an exception.

## Final Words

String matching is something very special in software development and it is used in various cases, so every developer must be familiar with this topic.

Topics:

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Published at DZone with permission of Stoimen Popov , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.