Longest Common Subsequence
Given two strings, find 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. For example, "ace" is a subsequence of "abcde" because we can delete "b" and "d". But "aec" is not, because the order changes. A common subsequence is a string that is a subsequence of both inputs.
word1 = "abcde", word2 = "ace"
3
Deleting "b" and "d" from "abcde" leaves "ace", which is all of word2, so the longest common subsequence has length 3.
word1 = "abcd", word2 = "dcba"
1
The two strings share all four characters, but reversing the order means no two of them appear in the same relative order in both strings. Any single character, such as "a", is a common subsequence, so the answer is 1.
1 <= word1.length, word2.length <= 1000word1andword2consist of lowercase English characters