Introduction
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 is great to understand their principles.
String algorithms can be mainly divided into several categories. One of these categories is string matching.
When we come to string matching the most basic approach is what is known as brute force, which means just 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 into 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 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 the second character of the pattern comparing it with the “next” character of the text, as on the picture bellow.
Just because we have found a match between the first character from the pattern with 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 into 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 naturally this algorithm is 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 is string matching 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 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 …
- 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!
- 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
- Can be quite effective for short texts and patterns
It can be ineffective …
- If we search more than once the text – 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.
- 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.
thnx man that was very helpful…
this is very helpful for the students as well as teachers.thanx .
Nicely Explained…..and content is also good…..Thanx…
Thanks….
Why to view the result :
Subject : HELLO WORD
Patt : O WO
Result:
___HELLO WORD
1. O WO
2. _O WO
3. __O WO
4. ___O WO
5. ____O WO
Very detailed explanation. Every picture in your blog gives more visualization about the concept. Please keep doing.
Thanks for your work. If possible, pls post your code in python as lot of people are using python nowadays.
thanks…………
This is really helpful. The explanation was very good and in depth. Any novice can get it easily.
Algorithm doesn’t match pattern at the end of the subject.
For example,
echo sub_string(‘br’, ‘habarahabr’);
returns -1
Also it doesn’t work with pattern equals subject.
For example,
echo sub_string(‘br’, ‘habarahabr’);
returns -1
To fix, just change for-loop
for ($i = 0; i <= $n-$m; $i++)
> Also it doesn’t work with pattern equals subject.
> For example,
> echo sub_string(‘br’, ‘br’);
> returns -1
I meant.
thanks
if you can also update the analysis part.
It cool, nice work.
Thank you for this!
algorith have error
for ($i = 0; i < $n-$m; $i++) {
test with string "cbcc" search pattern "cc"
for ($i = 0; i <= $n-$m; $i++) {
Your example is great but there is an issue with the code.
The for loop need to change from for ($i = 0; i < $n-$m; $i++) to for ($i = 0; i <= $n-$m; $i++).
Run this for example
sub_string("fgg", "abcdfffggffgg");
Just check before entering for loop that $Subject & $pattern is equels…
i need brute force algo for comparing my data sets with any random blog data from internet to find out data type. for example result will show after comparison the blog data belong to emtion type..or sexual abuse text …etc