Longest Common Subsequence of Three Strings

Given three strings A, B, and C, the task is to determine the length of the longest common subsequence across these three strings.

Explanation:

A subsequence of a string is a sequence that can be derived by deleting some characters (possibly zero) from the original string without changing the order of the remaining characters. For instance, "cde" is a subsequence of "code", but "cdo" is not. A common subsequence among multiple strings is one that appears in each of those strings.

Input:

The first line contains an integer 'T', representing the number of test cases. For each test case:
The first line contains three space-separated positive integers n, m, k, representing the lengths of strings A, B, and C respectively.
The second line contains the string A.
The third line contains the string B.
The fourth line contains the string C.

Output:

For each test case, return the length of the longest common subsequence among the three strings A, B, and C.

Example:

Input:
2
3 3 3
abc
abc
abc
4 4 4
abcd
aefg
bcdf
Output:
3
1
Explanation:

In the first test case, 'abc' is the common subsequence among all three strings, thus the length is 3. In the second test case, the common subsequence is 'c', so the length is 1.

Constraints:

  • 1 <= T <= 5
  • 1 <= n, m, k <= 100
  • n, m, k denote the lengths of strings A, B, and C, respectively.
  • Time limit: 1 second
Note:

You don’t need to print anything. Just complete the function. If there is no common subsequence, return 0.

Be the first one to answer
Add answer anonymously...
DE Shaw Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter