Closed leshow closed 7 years ago
Hi @leshow, good catch! You're right it seems to be implemented as longest common subsequence. Are you interested in making a pull request to change the existing challenge to properly label it as a subsequence?
For longest common substring, I think it would be more like below. This would be a new challenge.
...
elif str0[j - 1] != str1[i - 1]:
T[i][j] = 0
...
Sure! I'll make the necessary changes to the answer.
Personally, I think the solution makes more sense when the containing if
case tests for a match, a[i-1] == b[j-1]
, instead of doing the inequality test first, but I'll leave that the way it is.
https://en.wikipedia.org/wiki/Longest_common_substring_problem
according to wikipedia, because the substring is contiguous, we should end up with something like: (I don't know python, sorry)
Other solutions I've found also do something similar: http://www.geeksforgeeks.org/longest-common-substring/
However, the longest common substring implemented here does:
despite stating:
It looks as though the solution is modelling the recurrence relation for subsequence not substring. Have a look at the geeksforgeeks solution for subsequence:
http://www.geeksforgeeks.org/dynamic-programming-set-4-longest-common-subsequence/
It's very similar to this repo's implementation of 'substring'