Blind 75
60 / 75Free practice

Longest Common Subsequence

Medium · Dynamic programming

Jump to code

Find the length of the longest sequence obtainable from each of two strings by deleting characters without reordering those kept.

Examples

Example 1

text1 = "abcde"
text2 = "ace"

Output: 3

Example 2

text1 = "abc"
text2 = "abc"

Output: 3

Constraints

  • 1 <= text1.length, text2.length <= 1000
  • text1 and text2 consist of only lowercase English characters.
Need a hint?

Compare the final characters of two prefixes.

Asked in real interviews

Candidates reported versions of Longest Common Subsequence from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  "abcde",
  "ace"
]
Expected
3
Case 2
Input
[
  "abc",
  "abc"
]
Expected
3
Case 3
Input
[
  "abc",
  "def"
]
Expected
0
Case 4
Input
[
  "abc",
  "ac"
]
Expected
2
Case 5
Input
[
  "",
  "abc"
]
Expected
0

Drafts are saved per language in this browser when storage is available.