📘
Longest Common Subsequence
MediumDynamic Programming
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
abcdeandace, 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, ande. - The length of this LCS is 3, since it contains three characters.
- The final output is the length of the LCS, which is 3.
Constraints:
- 1 <= len(text1), len(text2) <= 1000
- text1 and text2 consist of lowercase English letters
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.