78. Longest Common Subsequence

Return the length of the longest subsequence present in both strings (characters in order, not necessarily adjacent). Return 0 if there is none.

Example 1

Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The LCS is “ace”.

Example 2

Input: text1 = "AGGTAB", text2 = "GXTXAYB"
Output: 4
Explanation: The LCS is “GTAB”.
Hint 1

Compare the last characters of the two prefixes.

Hint 2

If they match, the LCS grows by one; otherwise drop one character from either string.

Dynamic ProgrammingString