









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Given two strings s and t of lengths m and n, find the minimum window substring in s that contains all the characters of t (including duplicates). If no such substring exists, return an empty string "".
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!
-- The basic idea behind it is that the pattern and text are compared character by character.
-- In case if a character is not matched, then the pattern is shifted one position to the right and the comparison is repeated until a match is found or the end of the text is reached.
T[s + j] = P[j] for 1 j m,
such a shift is called a valid shift.
Algorithm naiveMatch_rec (T[ ], N, P[ ], M) if (N < M) then return 0; else if (M == -1) then return 1; else if (T[N] == P[M]) then return (naiveMatchRec (T, N-1, P, M-1)); else return (naiveMatchRec (T, N-1, P, M));
p = P[m] + 10(P[m-1] +10(P[m-2]+... +10(P[2]+10(P[1]))
m =
Computation of p and t 0 and the recurrence is done using modulus q.
computer word.
The recurrence equation can be rewritten as ts+1 = (d(ts –T[s+1]h)+ T[s+m+1]) mod q, where h = dm-1(mod q) is the value of the digit “1” in the high order position of an m- digit text window.
However, if ts is not equivalent to p mod q ,
Further testing is done to eliminate spurious hits.
ts+1 = (d(ts –T[s+1]h)+ T[s+m+1]) mod q h = dm-1(mod q) Example :
T = 31415; P = 26, n = 5, m = 2, q = 11
p = 26 mod 11 = 4 t0 = 31 mod 11 = 9 t1 = (10(9 - 3(10) mod 11 ) + 4) mod 11 = (10 (9- 8) + 4) mod 11 = 14 mod 11 = 3
Figure: The same text string with values computed modulo 13 for each possible position of a length-5 window. Assuming the pattern P = 31415, we look for windows whose value modulo 13 is 7, since 31415 7 (mod 13). Two such windows are found, shown shaded in the figure. The first, beginning at text position 7, is indeed an occurrence of the pattern, while the second, beginning at text position 13, is a spurious hit.
ts+1 = (d(ts - T[s + 1]h) + T[s + m + 1]) mod q
Procedure RABIN-KARP-MATCHER(T, P, d, q)
prime q. Output : valid shifts s where P matches
The loop of line 9 takes 𝜽((n-m+1)m) time The loop 6-8 takes O(m) time The overall running time is O((n-m)m)