CodePark/ problems / Longest Common Subsequence
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Dynamic programming

Longest Common Subsequence

mediumabout 25 minutes in an interview

Return the length of the longest common subsequence of two strings a and b.

A subsequence is what's left after you delete zero or more characters. The remaining characters keep their order. For example, "rve" is a subsequence of "brave", but "evr" is not. A common subsequence appears in both strings.

  • Either string may be empty. Then the answer is 0.
  • Each string has up to 1,000 lowercase letters.
  • Return only the length, not the subsequence itself.
Input: a = "brave", b = "bread" Output: 3"bra" is in both strings. So is "bre". No common subsequence of length 4 exists.
Input: a = "abc", b = "cba" Output: 1
Input: a = "drill", b = "" Output: 0