Longest Subsequence That Is a Substring
Problem statement
Given two strings x and y, return the maximum length of a subsequence of x that is also a contiguous substring of y.
A subsequence keeps the relative order of selected characters but may delete characters. A substring uses consecutive characters.
Function
longestSubsequence(x: String, y: String) → intExamples
Example 1
x = "abcd"y = "abdc"return = 3"abd" is a subsequence of x and a contiguous substring of y, so the answer is 3.
Example 2
x = "hackerranks"y = "hackers"return = 7"hackers" appears contiguously in y and can be selected in order from x.
Example 3
x = "abc"y = "aedace"return = 2"ac" is a subsequence of x and a substring of y. No qualifying value has length 3.
Constraints
1 <= x.length, y.length <= 2000.xandycontain only lowercase English letters.