Skip to content
Onsite

Longest Common Subsequence

MediumP1Coding

Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence keeps the original order of characters but may skip some; it does not need to be contiguous. If the strings share no characters, return 0.

The comparison is case-sensitive.

Example 1

Input
text1 = "abcde", text2 = "ace"
Output
3
Why
The subsequence "ace" has length 3.

Example 2

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

Example 3

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

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 11.