Two friends each encode the play order of one of their playlists as a string, where each character is a single-letter code for a track. Find the length of the longest sequence of track codes that appears, in the same relative order, in both playlists (the tracks do not need to be consecutive in either playlist).
Line 1: the first playlist s1 (may be empty, producing a blank line).
Line 2: the second playlist s2 (may be empty, producing a blank line).
Both strings contain only lowercase English letters.
A single integer: the length of the longest common subsequence of s1 and s2.
Example 1
Input
abcde ace
Expected
3
Explanation
"ace" appears in order within "abcde" (positions a-c-e), so the longest common subsequence has length 3.
Example 2
Input
abc xyz
Expected
0
Explanation
No character of "abc" appears anywhere in "xyz", so the longest common subsequence is empty, length 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →