Se - 31
Se - 31
STRING MATCHING
Session-31
String Matching
• Text-editing programs frequently need to find
all occurrences of a pattern in the text.
• Given a text is an array T[1..n] of length n and
a pattern P[1..m] of length m<=n.
• We say that pattern P occurs with shift s in
text T if 0≤s≤ n-m and T[s+1..s+m] = P[1..m].
• If P occurs with shift s in T , then we call s a
valid shift; otherwise, we call s an invalid shift.
String Matching
• The string-matching problem is the problem
of finding all valid shifts with which a given
pattern P occurs in a given text T .
• Example: T = ababcabdabcaabc and P = abc,
the occurrences are:
– first occurrence starts at T[3]
– second occurrence starts at T[9]
– third occurrence starts at T[13]
• Applications:
– Searching keywords in a file
– Searching engines (like Google and
Openfind)
– Database searching (GenBank)
The naive string-matching algorithm
T: a b c a b d a a b c d e
P: a b d
Example ( Step – 1 )
T: a b c a b d a a b c d e
P: a b d
T: a b c a b d a a b c d e
a b d
P:
T: a b c a b d a a b c d e
a b d
P:
T: a b c a b d a a b c d e
a b d
P: