Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence keeps the original left-to-right order of characters, but the chosen characters do not need to be adjacent.
Input / output
text1: str, text2: strint — the length of the longest subsequence common to both stringsExamples
text1 = "abcde", text2 = "ace" returns 3 because "ace" is a common subsequence.text1 = "abc", text2 = "abc" returns 3 because the entire string matches.text1 = "abc", text2 = "def" returns 0 because they share no characters in order.Constraints
0 <= text1.length, text2.length <= 1000text1 and text2 contain only lowercase English lettersFollow-up
How would you reconstruct one actual longest common subsequence string, not just its length? Can you reduce the dynamic-programming table from O(m * n) space to O(min(m, n)) space?