GATE | GATE-CS-2014-(Set-2) | Question 47

Consider two strings A = “qpqrr” and B = “pqprqrp”. Let x be the length of the longest common subsequence (not necessarily contiguous) between A and B and let y be the number of such longest common subsequences between A and B. Then x + 10y = ___.
(A) 33
(B) 23
(C) 43
(D) 34

Answer: (D)

Explanation: //The LCS is of length 4. There are 3 LCS of length 4 “qprr”, “pqrr” and qpqr


A subsequence is a sequence that can be derived from another sequence by selecting zero or more elements from it, without changing the order of the remaining elements. Subsequence need not be contiguous. Since the length of given strings A = “qpqrr” and B = “pqprqrp” are very small, we don’t need to build a 5×7 matrix and solve it using dynamic programming. Rather we can solve it manually just by brute force. We will first check whether there exist a subsequence  of length 5 since min_length(A,B) = 5.

Since there is no subsequence , we will now check for length 4. “qprr”, “pqrr” and “qpqr” are common in both strings.

X = 4 and Y = 3

X + 10Y = 34


This solution is contributed by Pranjul Ahuja

Quiz of this Question

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:

0 Average Difficulty : 0/5.0
No votes yet.