0% found this document useful (0 votes)
49 views5 pages

String Matching Algorithm

Uploaded by

atifansary18
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
49 views5 pages

String Matching Algorithm

Uploaded by

atifansary18
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 5

STRING MATCHING

ALGORITHM
INTRODUCTION
◦ Given a text array, T [1…..n], of n character and a pattern array, P [1……m], of m
characters. The problems are to find an integer s, called a valid shift where 0 ≤ s < n-
m and T [s+1……s+m] = P [1……m]. In other words, to find even if P in T, i.e.,
where P is a substring of T. The items of P and T are characters drawn from some
finite alphabet such as {0, 1} or {A, B …..Z, a, b….. z}.
◦ The substrings of a string T [1……n] are represented as T [i……j] for some 0i jn-1,
the string generated by the characters in T from index i to index j, inclusive. This
method assumes that a string is a substring of itself (i = 0 and j = m). For some 0i jn-
1, the correct substring of string T [1……n] is T [1……j]. That is, either i>0 or j m-1
must exist.
TYPES OF STRING MATCHING ALGORITHM

1. Brute Force Method


The brute force approach is the simplest string matching algorithm. It involves comparing the
pattern with every substring of the text until a match is found. This algorithm has a time
complexity of O(mn), where ‘m’ is the length of the pattern and ‘n’ is the length of the text.
Although straightforward, it becomes inefficient for large texts or patterns.

2. Knuth-Morris-Pratt (KMP) Algorithm


The KMP algorithm improves upon the brute force method by utilizing information from previous
comparisons to avoid unnecessary character comparisons. It precomputes a prefix function that
helps determine the number of characters to skip in the pattern whenever a mismatch occurs. This
results in a time complexity of O(n + m) for string matching.
3. Boyer-Moore Algorithm
The Boyer-Moore algorithm is a powerful string matching algorithm that takes advantage of both the bad
character rule and the good suffix rule. It examines the text from right to left and shifts the pattern more
intelligently based on mismatched characters. This algorithm has an average time complexity of O(n/m) for
string matching.

4. Rabin-Karp Algorithm
The Rabin-Karp algorithm is a popular string matching algorithm that utilizes hashing. It compares the
hash values of the pattern and each substring of the text to determine potential matches. This algorithm has
an average time complexity of O(n + m), where ‘n’ is the length of the text and ‘m’ is the length of the
pattern

5. DFA (Deterministic Finite Automaton) Method


The DFA method constructs a finite automaton that represents the pattern to be matched. By traversing the
automaton for each character in the text, this algorithm efficiently identifies matches. It has a time
complexity of O(n) for preprocessing the pattern and O(m) for matching, making it highly efficient for
large texts.

You might also like