FastPrepLongest Subsequence That Is a Substring

Longest Subsequence That Is a Substring

Wells Fargo logoWells Fargo● MediumFULLTIMEOA
Learn

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) → int

Examples

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.
  • x and y contain only lowercase English letters.

More Wells Fargo problems

See Wells Fargo hiring insights
public int longestSubsequence(String x, String y) {
  // Write your code here
}
x"abcd"
y"abdc"
expected3
Checking account…