Longest Common Subsequence Length
Problem statement
Return the length of the longest common subsequence of first and second. A subsequence is obtained by deleting zero or more characters without changing the order of those kept. Kept characters do not need to be adjacent.
For this exercise, assume the inputs are strings of uppercase and lowercase English letters. Compare characters exactly and allow repeated letters; each position can be selected at most once.
For this exercise, assume you return only the maximum length, not the subsequence itself. Return 0 when either string is empty or the strings share no character.
Function
longestCommonSubsequence(first: String, second: String) → intExamples
Example 1
first = "abcde"second = "ace"return = 3The characters a, c and e occur in this order in both strings. They need not be adjacent.
Example 2
first = "abc"second = "cab"return = 2The subsequence ab has length two. No three-character subsequence matches both orders.
Example 3
first = "Ab"second = "ab"return = 1Only lowercase b matches under case-sensitive equality.
Constraints
- For this exercise, assume
0 <= first.length, second.length <= 500. - Every character is an uppercase or lowercase English letter; equality is case-sensitive.