Back to DSA

Longest Common Subsequence

medium
Acceptance: 51%
Dynamic Programming

Given two strings, determine the length of their longest common subsequence. A subsequence preserves relative order but need not consist of consecutive characters. Return 0 if no common subsequence exists.

Examples

Example 1:
Input:text1 = "mango", text2 = "magnolia"
Output:4
Explanation: "mago" is a common subsequence of length 4.
Example 2:
Input:text1 = "xyz", text2 = "abc"
Output:0
Explanation: The two strings share no characters.

Hints

00:00
1234567