0% completed
Problem Statement
Given two strings 's1' and 's2', find the length of the longest subsequence which is common in both the strings.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: s1 = "abdca"
s2 = "cbda"
Output: 3
Explanation: The longest common subsequence is "bda".
Example 2:
Input: s1 = "passport"
s2 = "ppsspt"
Output: 5
Explanation: The longest common subsequence is "psspt".
Constraints:
- 1 <= s1.length, s2.length <= 1000`
s1
ands2
consist of only lowercase English characters.
Basic Solution
A basic brute-force solution could be to try all subsequences of 's1' and 's2' to find the longest one. We can match both the strings one character at a time. So for every index 'i' in 's1' and 'j' in 's2' we must choose between:
- If the character
s1[i]
matchess2[j]
, we can recursively match for the remaining lengths. - If the character
s1[i]
does not matchs2[j]
, we will start two new recursive calls by skipping one character separately from each string.
Here is the code:
The time complexity of the above algorithm is exponential O(2^{m+n}), where 'm' and 'n' are the lengths of the two input strings. The space complexity is O(n+m) which is used to store the recursion stack.
Top-down Dynamic Programming with Memoization
We can use an array to store the already solved subproblems.
The two changing values to our recursive function are the two indexes, i1 and i2. Therefore, we can store the results of all the subproblems in a two-dimensional array. (Another alternative could be to use a hash-table whose key would be a string (i1 + "|" + i2)).
Here is the code:
Bottom-up Dynamic Programming
Since we want to match all the subsequences of the given two strings, we can use a two-dimensional array to store our results. The lengths of the two strings will define the size of the array's two dimensions. So for every index 'i' in string 's1' and 'j' in string 's2', we will choose one of the following two options:
- If the character
s1[i]
matchess2[j]
, the length of the common subsequence would be one plus the length of the common subsequence till thei-1
andj-1
indexes in the two respective strings. - If the character
s1[i]
does not matchs2[j]
, we will take the longest subsequence by either skipping ith or jth character from the respective strings.
So our recursive formula would be:
if s1[i] == s2[j]
dp[i][j] = 1 + dp[i-1][j-1]
else
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
Let's draw this visually for "abcda" and "cbda". Starting with a subsequence of zero lengths, if any string has zero length then the common subsequence will be of zero length:
![i:0, j:0-5 and i:0-4, j:0 => dp[i][j] = 0, as we don't have any common subsequence when one of the string is of zero length](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f5c8ba842de869cd6272b%252Fimg%3A70ab4-3fe0-0580-aafe-8171ffe54b65.jpg%3Fgeneration%3D1669291452803623%26alt%3Dmedia&w=3840&q=75&dpl=dpl_3PMqp3jvFBB168VSnRL2UGAbQz2w)
![i:1, j:1 => dp[i][j] = max(dp[i-1][j], dp[i][j-1]), as s1[i] != s2[j]](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f5c8ba842de869cd6272b%252Fimg%3A860b8f3-4417-160-d2b-782c3bc0d0a.jpg%3Fgeneration%3D1669291501783398%26alt%3Dmedia&w=3840&q=75&dpl=dpl_3PMqp3jvFBB168VSnRL2UGAbQz2w)
![i:1, j:4 => dp[i][j] = 1 + dp[i-1][j-1], as s1[i] == s2[j]](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f5c8ba842de869cd6272b%252Fimg%3Add6fb1-ac5b-06fb-cfd2-0b384fc5a3b4.jpg%3Fgeneration%3D1669291528905737%26alt%3Dmedia&w=3840&q=75&dpl=dpl_3PMqp3jvFBB168VSnRL2UGAbQz2w)
![i:1, j:5 => dp[i][j] = max(dp[i-1][j], dp[i][j-1]), as s1[i] != s2[j]](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f5c8ba842de869cd6272b%252Fimg%3A0c6127d-3f5e-6b7c-cddc-404524c2242.jpg%3Fgeneration%3D1669291554869408%26alt%3Dmedia&w=3840&q=75&dpl=dpl_3PMqp3jvFBB168VSnRL2UGAbQz2w)
![i:2, j:2=> dp[i][j] = 1 + dp[i-1][j-1], as s1[i] == s2[j]](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f5c8ba842de869cd6272b%252Fimg%3A2704bc-0e6-6c88-d3c0-376a4b1fe84.jpg%3Fgeneration%3D1669291589344875%26alt%3Dmedia&w=3840&q=75&dpl=dpl_3PMqp3jvFBB168VSnRL2UGAbQz2w)
![i:2, j:3-5 => dp[i][j] = max(dp[i-1][j], dp[i][j-1]), as s1[i] != s2[j]](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f5c8ba842de869cd6272b%252Fimg%3Adc5b63e-16-cc2-e30d-e0de685aa20.jpg%3Fgeneration%3D1669291621756396%26alt%3Dmedia&w=3840&q=75&dpl=dpl_3PMqp3jvFBB168VSnRL2UGAbQz2w)
![i:3, j:3=> dp[i][j] = 1 + dp[i-1][j-1], as s1[i] == s2[j]](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f5c8ba842de869cd6272b%252Fimg%3A3d0a7a7-1465-b8f4-b30-f172f0ceecb2.jpg%3Fgeneration%3D1669291673257507%26alt%3Dmedia&w=3840&q=75&dpl=dpl_3PMqp3jvFBB168VSnRL2UGAbQz2w)
![i:4, j:5=> dp[i][j] = 1 + dp[i-1][j-1], as s1[i] == s2[j]](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f5c8ba842de869cd6272b%252Fimg%3Af85f6f-851-03d4-187d-0e5d672c18.jpg%3Fgeneration%3D1669291767124854%26alt%3Dmedia&w=3840&q=75&dpl=dpl_3PMqp3jvFBB168VSnRL2UGAbQz2w)
![i:4, j:5=> dp[i][j] = 1 + dp[i-1][j-1], as s1[i] == s2[j]](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f5c8ba842de869cd6272b%252Fimg%3Ab5301e6-ce4d-3f-ab71-ac7d5223fcf.jpg%3Fgeneration%3D1669291788981794%26alt%3Dmedia&w=3840&q=75&dpl=dpl_3PMqp3jvFBB168VSnRL2UGAbQz2w)
From the above visualization, we can clearly see that the longest common subsequence is of length '3' -- as shown by dp[4][5]
.
Here is the code for our bottom-up dynamic programming approach:
The time and space complexity of the above algorithm is O(m*n), where 'm' and 'n' are the lengths of the two input strings.
Challenge
Can we further improve our bottom-up DP solution? Can you find an algorithm that has O(n) space complexity?
Hint
We only need one previous row to find the optimal solution!
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible