You have been given two Strings “STR1” and “STR2” of characters. Your task is to find the length of the longest common subsequence.
A String ‘a’ is a subsequence of a String ‘b’ if ‘a’ can be obtained from ‘b’ by deletion of several (possibly, zero or all) characters. A common subsequence of two Strings is a subsequence that is common to both Strings.
The first line contains an integer ‘T’ denoting the number of test cases. Then each test case follows.
The first input line of each test case contains two space-separated Strings “STR1” and “STR2” representing two Strings.
Output Format :
For each test case, print the length of the longest common subsequence.
Print the output of each test case in a separate line.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= |STR1| <= 10^2
1 <= |STR2| <= 10^2
Where |STR1| and |STR2| denote the length of “STR1” and “STR2” respectively.
Time Limit: 1 sec
2
abc cadb
pqr tpuqvr
2
3
For the first test case, the longest common subsequence is “ab” and its length is 2.
For the second test case, the longest common subsequence is “pqr” and its length is 3.
2
a b
a a
0
1
For the first test case, any non-empty common subsequence doesn’t exist.
For the second test case, the longest common subsequence is “a” and its length is 1.
Can you think of breaking the problem into sub-problems?
The basic idea of this approach is to break the original problem into sub-problems. Let us assume we want to find the length of the longest common subsequence of “STR1” and “STR2” whose length is ‘N’ and ‘M’ respectively.
Now, let us define a recursive function
LCS(Int I, int J, string STR1, string STR2)
Which returns the length of the longest common subsequence of string STR1[0: I-1] and STR1[0: J-1] where STR1[l: R] denotes the substring of “STR1” having characters from index ‘l’ to index ‘R’.
Now, consider the following steps :
O(2^(N+M), where ‘N’ and ‘M’ are the length of 'STR1' and ‘STR2’ respectively.
Since we are using a recursive function to find the length of LCS where we are comparing each subsequence of the first string with the second string. So the overall time complexity will be O(2^(N + M)).
O(max(N, M)), where ‘N’ and ‘M’ are the length of 'STR1' and ‘STR2’ respectively.
Since we are using a recursive function to find the length of LCS and in the worst case, there may be max(N, M) state in the call stack. So the overall space complexity will be O(max(N, M)).