Find the length of the longest subsequence that appears in both strings.
Given two strings text1 and text2, determine 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. The characters of the subsequence do not need to be contiguous.
Your task is to compute the maximum possible length of a string that is a subsequence of both text1 and text2.
text1 and text2.Example 1
Input
text1 = "abcde", text2 = "ace"
Output
3
Explanation
The longest common subsequence is "ace", which has length 3.
Example 2
Input
text1 = "abc", text2 = "abc"
Output
3
Explanation
The entire string is common, so the LCS length is 3.
Example 3
Input
text1 = "abc", text2 = "def"
Output
0
Explanation
The strings share no common subsequence except the empty one.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.