Longest Common Subsequence

Given two strings text1 and text2, return the length of their longest common subsequence. The subsequence must appear in both strings in the same relative order, but it does not need to be contiguous. Pattern focus: LCS. This is the classic 2D DP formulation for subsequence matching.

Input Format

text1 = first string, text2 = second string

Output Format

length of the longest common subsequence

Constraints

  • 0 <= text1.length, text2.length <= 1000
  • text1 and text2 contain lowercase English letters.

Examples

Example 1:

Input:

text1 = "abcde"
text2 = "ace"

Output:

3

Explanation:

The LCS is ace.

Example 2:

Input:

text1 = "abc"
text2 = "def"

Output:

0

Explanation:

The strings have no common subsequence.

Loading...
Longest Common Subsequence - Dp Strings