Skip to content

Longest Common Subsequence

MediumDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

Return the length of the longest subsequence that appears in both strings a and b. A subsequence keeps the original order but may skip characters. Return 0 if they share nothing.

a = "abcde", b = "ace"  →  3   ("ace")
a = "abc",   b = "abc"  →  3
a = "abc",   b = "def"  →  0

Constraints

  • 0 <= a.length, b.length <= 1000
  • Both strings contain only lowercase letters.