Sunna is very sad because she can't think of the lyrics for her idol group's upcoming song, so you decided to help her by connecting the common subsequence between current songs to create a new song for her!

A subsequence of a string is obtained by deleting zero or more characters without changing the relative order of the remaining character
For example, consider the string abcde
abcde and the empty string are also valid subsequences.Note that the characters of a subsequence do not need to be consecutive.
A Longest Common Subsequence (LCS) of two strings is the longest subsequence that appears in both strings.
For example
X = "abcbdab" Y = "bdcaba"
One possible LCS is bcba and the length of this common subsequence is 4
X = a b c b d a b Y = b d c a b a
Step1 - Brute-force recursion
Let lcs(i, j) represent the length of the LCS between the first i characters of X and the first j characters of Y.
By comparing the last characters of both prefixes, the problem can be divided into smaller subproblems.
def lcs(i, j):
if i == 0 or j == 0:
return 0
elif X[i-1] == Y[j-1]:
return 1 + lcs(i-1, j-1)
else:
return max(lcs(i-1, j),
lcs(i, j-1))
answer = lcs(len(X), len(Y))
Although this approach is correct, the same subproblem (i, j) may be solved multiple times
For example, consider X = "agcat" and Y = "gac":
lcs(5, 3)
/ \
lcs(4, 3) lcs(5, 2)
/ \ / \
lcs(3, 3) lcs(4, 2) lcs(4, 2) lcs(5, 1)
↑ ↑
Same subproblem
As the strings become longer, repeated computations can lead to exponential time complexity.
Step2 - Avoid repeated computation
Instead of solving the same subproblems repeatedly, consider storing their results in a two-dimensional table dp.
Let dp[i][j] represent the result of lcs(i, j).
Each cell depends only on its upper-left, upper, or left neighbour.
Fill the table in an order that ensures the required results have already been computed.
After completing the table, dp[n][m] contains the final answer.
This approach is known as Dynamic Programming (DP) and reduces the time complexity to O(n × m).
Example - Building the DP table
Consider X = "AGCAT" and Y = "GAC".
dp[1][2] represents the LCS length of "A" and "GA".
dp[3][3] represents the LCS length of "AGC" and "GAC".
dp[4][3] represents the LCS length of "AGCA" and "GAC".
Think about how the recursive approach from Step 1 can be applied to fill each cell without making repeated recursive calls.
The final answer is dp[5][3] = 2.
Therefore, the length of the longest common subsequence is 2.
def longest_common_subsequence(X, Y):
n = len(X)
m = len(Y)
# prev[j]: LCS length for X[:i-1] and Y[:j].
# curr[j]: LCS length for X[:i] and Y[:j].
prev = [0] * (m + 1)
curr = [0] * (m + 1)
for i in range(1, n + 1):
x = X[i - 1]
curr[0] = 0
for j, y in enumerate(Y, 1):
# TODO: Compute curr[j] by comparing x and y
# and using previously computed results.
pass
# Reuse the two rows for the next iteration.
prev, curr = curr, prev
return prev[m]
if __name__ == "__main__":
X = input()
Y = input()
print(longest_common_subsequence(X, Y))
The input consists of two lines:
The first line contains a string X of length n.
The second line contains a string Y of length m.
Both strings consist of lowercase English letters and digits, without spaces.
Either string may be empty.
0 ≤ n, m ≤ 2000
Both strings contain only lowercase English letters and digits.
Time Limit: 1 second.
Print one integer representing the length of the longest common subsequence of X and Y.