SalesforceOA #12
Find Longest Subsequence Common to X as Substring in Y
Estimated MediumJudge-ready
Topics (AI-labelled)String DPTwo Pointers
Find the longest string s that is both a subsequence of X and a contiguous substring of Y, and return its length. The original text of this entry has only a title, function name, and examples. When there is no match, the length is 0.
Input
Two lines, X and Y respectively. On this site both are non-empty lowercase strings of length 1..2000.
Output
Output the maximum length.
Example 1
Input:
hackerranks
hackers
Output:
7
Example 2
Input:
abcd
abdc
Output:
3
Example 3
Input:
abc
zz
Output:
0
Example notes
The answers are 7, 3, and 0; in the second example, abd is a subsequence of X and a contiguous substring of Y.
Editorial
Loading solution…