Matching Techniques


Matching Substrings

Q. Write an efficient algorithm for matching substrings. (Dec. 02)

i: = 0
while I < (n - m + 1) do
begin
i: = i + 1; j: = I; k: = 1;
while S1(j) = S2(k) do
begin
if k = m writeln('success')
else do
begin
j: = j + 1; k: = k + 1
end
end
end
writeln('fail')
end

In the above algorithm, i and j are the position indices for strings S1 and k a position index for S2. This algorithm requires m(n - m) comparisions in the worst case.

 
          AI Contents  
©Universal Teacher Publications Web: www.universalteacherpublications.com.