Longest Common Subsequence

Dynamic Programming, problem 7 of 9

Longest Common Subsequence

Medium

LC #1143

two strings2d dp

Not attempted yet

Return the length of the longest common subsequence of text1 and text2, or 0 if they share none. A subsequence keeps the original order but may skip characters ("ace" is a subsequence of "abcde").

Example 1

Input: text1 = "abcde", text2 = "ace"
Output: 3
"ace"

Example 2

Input: text1 = "abc", text2 = "abc"
Output: 3

Example 3

Input: text1 = "abc", text2 = "def"
Output: 0

Constraints

  • 1 ≤ len(text1), len(text2) ≤ 1000
  • lowercase letters only

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.