SalesforceOA #25
Longest Subsequence which is a Substring
Estimated MediumJudge-ready
Topics (AI-labelled)String DPTwo Pointers
Find a string that is a subsequence of X and also a contiguous substring of Y, and return its maximum length. Characters of X may be skipped, but characters of Y may not; when there is no common character, output 0.
Input
The input is a JSON array [X,Y]. The original gives no numeric or character domain; this site adds that each string has 0..1000 Unicode scalar values, and empty strings, whitespace, control characters, and non-BMP characters are allowed; lone surrogates are not included. The total UTF-8-encoded JSON input is ≤25000 bytes.
Output
Output one integer answer.
Example 1
Input:
["abcd","abdc"]
Output:
3
Example 2
Input:
["hackerranks","hackers"]
Output:
7
Example 3
Input:
["","abc"]
Output:
0
Example notes
The outputs of the three public examples are, in order: 3; 7; 0. They have been checked by independent enumeration or direct simulation.
Editorial
Loading solution…