Consider two strings = "qpqrr" and = "pqprqrp". Let be the length of the longest common subsequence (not necessarily contiguous) between and and let be the number of such longest common subsequences between and . Then ___.
View worked solution
Answer: 34
To find the length () and the number () of the longest common subsequences (LCS) between and :
Since the length of is , any subsequence of of length is obtained by deleting exactly one character from . Let's list all unique subsequences of of length :
- Delete character (): "pqrr"
- Delete character (): "qqrr"
- Delete character (): "qprr"
- Delete or character (): "qpqr"
Now, we check which of these are also subsequences of :
- "pqrr": Yes, matched at indices of ().
- "qqrr": No, because matching two 's (at indices and ) leaves only one (at index ) before the end of .
- "qprr": Yes, matched at indices of ().
- "qpqr": Yes, matched at indices of ().
The only subsequence of length is itself, which is not a subsequence of . Thus, the length of the LCS is , and the number of distinct longest common subsequences is (which are "pqrr", "qprr", and "qpqr").
Calculating the required value:
Free practice · Your answers on this page are not saved.
