Skip to interview prep
Apexed

48. Longest Common Subsequence

MediumDynamic programming

Return the length of the longest subsequence (characters in order, not necessarily adjacent) common to strings a and b.

Example 1

Input: a = "abcde", b = "ace"
Output: 3
Explanation: "ace"

Example 2

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

Constraints

  • 1 ≤ a.length, b.length ≤ 1000

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Longest Common Subsequence — coding interview practice — Apexed