You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
create a 2D array (dp) to store the length of the longest common subsequence upto that point.
We will iterate over the 2 strings and if the characters are same, we will add 1 to the length of the LCS upto the previous character.
If the characters are not same, we will take the maximum of the LCS upto the previous character of the first string and the LCS upto the previous character of the second string.
Time Complexity : O(N*M) // N and M are the lengths of the 2 strings
Space Complexity : O(N*M)
Approach-02 (Bottom Up - Recursive) :
create a 2D array (dp) to store the length of the longest common subsequence upto that point.
recursively call the function with the 2 strings and the lengths of the 2 strings.
if the lengths of the 2 strings are 0, return 0.
if the length of the 2 strings are not 0, check if the characters at the end of the 2 strings are same.
if they are same, add 1 to the length of the LCS upto the previous character.
if they are not same, take the maximum of the LCS upto the previous character of the first string and the LCS upto the previous character of the second string.