AlgoFrames

Longest common subsequence

Standard · Write code · about 15 min

Return the length of the longest common subsequence of text1 and text2. A subsequence keeps the original order but may skip characters. Return 0 when the strings share none.

Example 1

Input:

["abcde","ace"]

Output:

3

The common subsequence is ace.

Example 2

Input:

["abc","def"]

Output:

0

No character appears in both.

Constraints

  • 1 ≤ text1.length, text2.length ≤ 1000
  • Both strings use lowercase English letters.

Learn the idea first: String DP: LCS, edit distance, palindromic subsequence, a visual lesson.

More String DP: LCS, edit distance, palindromic subsequence problems