v9.1.1
Menu

Longest Common Subsequence

mediumFREEDynamic ProgrammingBlind 75

Given two strings, return the length of their longest common subsequence (LCS). A subsequence maintains relative order but needn't be contiguous.

Example

Input
abcde
ace
Output
3
Reasoning
  • The two input strings are abcde and ace, and we need to find their longest common subsequence (LCS).
  • We compare the characters of both strings and find the common characters in the same relative order: a, c, and e.
  • The length of this LCS is 33, since it contains three characters.
  • The final output is the length of the LCS, which is 33.

Constraints

  • 1 <= len(text1), len(text2) <= 1000
  • text1 and text2 consist of lowercase English letters

Learn this in

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.