Longest Common Subsequence Problem

Longest Common Subsequence Problem — ExecCode Medium DSA Practice

Solve the Longest Common Subsequence problem on ExecCode. Free online medium DSA practice in DP. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given two strings text1 and text2, return the length of their longest common subsequence (LCS). A subsequence is formed by deleting zero or more characters without changing the order of the remaining characters. A common subsequence appears in both strings. If there is no common subsequence, return 0. Example idea: for text1 = "abcde" and text2 = "ace", one LCS is "ace" with length 3.

Examples

Input text1 = "abcde", text2 = "ace"; Output 3. Input text1 = "abc", text2 = "abc"; Output 3. Input text1 = "abc", text2 = "def"; Output 0

Constraints

1 ≤ text1.length, text2.length ≤ 1000

Practice Longest Common Subsequence free on ExecCode. Browse DSA problems, topic map, and placement guides.