FastPrepLongest Common Subsequence Length

Longest Common Subsequence Length

Zscaler logoZscaler● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

Given two lowercase strings first and second, return the length of their longest common subsequence.

A subsequence is formed by deleting zero or more characters without changing the relative order of the remaining characters. A common subsequence appears in both strings; its characters do not need to be consecutive.

Function

longestCommonSubsequence(first: String, second: String) → int

Examples

Example 1

first = "abcde"second = "ace"return = 3

The subsequence ace appears in both strings.

Example 2

first = "abc"second = "def"return = 0

The strings have no character in common.

Example 3

first = ""second = "abc"return = 0

An empty string has no nonempty subsequence.

Constraints

  • 0 <= first.length, second.length <= 1000
  • Both strings contain only lowercase English letters.

More Zscaler problems

See Zscaler hiring insights
public int longestCommonSubsequence(String first, String second) {
  // write your code here
}
first"abcde"
second"ace"
expected3
Checking account…