Skip to content
Decoder

Longest Common Subsequence

Medium

Problem

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.

Example 1

Input
a = "abcde", b = "ace"
Output
3
Why
"ace"

Example 2

Input
a = "abc", b = "abc"
Output
3

Example 3

Input
a = "abc", b = "def"
Output
0

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.