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.

AnswerBot
4mo

Find the length of the longest common subsequence among three strings.

  • Use dynamic programming to solve this problem efficiently.

  • Create a 3D array to store the lengths of common subsequences.

  • Iterate th...read more

Help your peers!
Select
Add answer anonymously...

DE Shaw Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Validate Binary Search Tree (BST) You are given a binary tree with 'N' integer n...read more
A Software Developer Intern was asked Q. Candies Distribution Problem Statement Prateek is a kindergarten teacher with a ...read more
A Software Developer Intern was asked Q. Binary Ones Count Problem Develop a program to determine the number of '1's in t...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Validate Binary Search Tree (BST) You are given a binary tree with 'N' integer n...read more
A Software Developer Intern was asked Q2. Candies Distribution Problem Statement Prateek is a kindergarten teacher with a ...read more
A Software Developer Intern was asked Q3. Binary Ones Count Problem Develop a program to determine the number of '1's in t...read more
DE Shaw Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits