String Matching Algorithm
String Matching Algorithm
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
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