Paths Subjects Questions Quizzes Pricing Search
Intermediate Open Pro

Longest Common Subsequence

Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence is a sequence formed by deleting some (or no) characters from a string without changing the relative order of the remaining characters, and a common subsequence is one shared by both strings. If there is no common subsequence, return 0.

Example 1

Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace", length 3.

Example 2

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

Example 3

Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no common subsequence.

Constraints

  • 1 <= text1.length, text2.length <= 1000
  • Both strings consist only of lowercase English characters.

Share this question

← Back to Dynamic Programming practice

We use cookies for product analytics to improve OmniAtlas. See our Privacy Policy.