I have found an algorithm for Longest Common Substring. It is usually done using dynamic programming
, using a 2-D array of size mxn
where m
and n
are lengths of the two strings under consideration.
I will construct the following matrix for the two strings.
M[i][j] = 1 if s1[i]==s2[j] else 0.
For example, if the strings are: abcxy
and pqaabx
The matrix looks as follows:
a b c x y
p 0 0 0 0 0
q 0 0 0 0 0
a 1 0 0 0 0
a 1 0 0 0 0
b 0 1 0 0 0
x 0 0 0 1 0
Now, I search for a maximal continuous sequence of 1
s in every diagonal which is in top-left to bottom-right direction.
The maximum value among these will be the answer.
I can perform the above operation without using the array explicitly. The time-complexity is still O(M*N)
. So, there is no need of memory.
Can anyone point me where I am going wrong?