JB TAK FODEGA NHI .... TB TK CHODEGA NHI .... (MAANG)
DPL27 Longest Common Substring 🔥
Longest Common Substring
A substring of a string is a subsequence in which all the characters are consecutive. Given two strings, we need to find the longest common substring.
We need to print the length of the longest common substring.
Again this Problem is the Modified Version of the DPL25 and DPL26
According to the Longest Common Subsequence Tabulation Formula is
Why this Same Formula Not Work in this Problem
If We CareFully Observed then we See, So Problem is this: Not Matching: max(dp[i-1][j],dp[i][j-1]) Omating or Including or Excluding Concept when the str1[index1] != str2[index2] Becouse this Formual Break the Rule of the Consecutive String As we See in the Img.
Some Important Point that we Observed During Crafting the Table
Tabulation Approch
Tabulation is a ‘bottom-up’ approach where we start from the base case and reach the final answer that we want and Memoization is the Top-down Approch.In Tabulation Approch We Just Creat a DP Array Same as Memoization and Simply Convert the Recurance Relation into the form of the Looping
Steps to convert Recursive Solution to Tabulation one.Note: dp[n][m] will not give us the answer; rather the maximum value in the entire dp array will give us the length of the longest common substring. This is because there is no restriction that the longest common substring is present at the end of both the strings.
Sb Mai He Kru ...
Khud Bhi Kr le Khuch ..... Nalayk
Time & Space Complexity
Time Complexity: O(N*M)Reason:There are 2 nested loops
Space Complexity: O(N*M)
Reason: We are using an external array of size ‘N*M’. Stack Space is eliminated.
Space Optimization
If we closelly Observed if any Tabulation Approch we used the Some Limited Stuff like: dp[ind1-1][ ], dp[ind][ ] for the finding the our ans then definetly here Spaced Optimization is Possible in that types of Problems. Always Remember
Golden RuleSb Mai He Kru ...
Khud Bhi Kr le Khuch ..... Nalayk
Time & Space Complexity
Time Complexity: O(N*M)Reason: There are three 2 nested loops
Space Complexity: O(M)
Reason: We are using an external array of size ‘M+1’ to store only two rows.