Longest common substring using Dynamic Programming | bottom-up approach

preview_player
Показать описание
Few years ago, I developed a yearning to grow skills in programming I came across an article which was about the DNA match of two persons.
It was stated in the article that the two DNAs are considered as very very long strings and then the job is to find common substring in two strings, once the
common substring is found the DNAs match.

It intrigued to solve this longest common substring problem on my own. With my limited talent, I came up with a naive solution of finding the longest substring between two strings. It
was working fine when the length of the strings were small but once I tested my program on strings of length beyond 200 chars, my computer jammed.

This poked me to write an optimized algorithm to find the longest common substring. I struggled and struggled until I came across this dynamic programming solution which appeared to me as nothing less than magic.

Here's the description of the problem -

Given two strings
s1 - ABCXYZAY
s2 - XYZABCD

we need to find the length of the longest common substring as well as the common substring between two strings which will be XYZA with length as 4.

This problem is different from longest common subsequence dynamic programming problem as a common substring is characters appearing continuously after one another
whereas, the common subsequence is characters appearing in the some order but not necessarily continuously.

So, let's watch this video to solve the longest common substring problem using dynamic programming.
Рекомендации по теме
Комментарии
Автор

awesome! thank you for showing the individual steps in detail.

elshroomness
Автор

I think describing the intuition is what helps.

shreyasheranjal
Автор

class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
n=len(nums1)
m=len(nums2)
dp=[[0]*(n+1) for i in range(m+1)]
for i in range(1, m+1):
for j in range(1, n+1):
if nums1[j-1]==nums2[i-1]:
dp[i][j]=dp[i-1][j-1]+1
else:
dp[i][j]=0
return max(map(max, dp))
to find the length of longest substring

hemanthpidugu
Автор

Pls do put questionn link related to video .It would be of really usefull.TIA really great videos

shriramr