Given two strings
text1andtext2, return the length of their longest common subsequence.
A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.
If there is no common subsequence, return 0.
Constraints:
1 <= text1.length <= 10001 <= text2.length <= 1000- The input strings consist of lowercase English characters only.
給兩個字串
text1和text2, 回傳它們最長共同子字串的長度。
一個字串的子字串是從原始字串生成的新字串,其中刪除了一些字元(可以是空字元)而不改變其餘字元的相對順序。(例如:"ace"是"abcde"的子字串,而"aec"則不是)。兩個字串的共同子字串是指該子字串同時為兩者的子字串。
如果沒有任何共同子字串,回傳0。
限制:
1 <= text1.length <= 10001 <= text2.length <= 1000- 輸入字串只會包含小寫英文字母。
m和n分別為兩個字串的長度。m + 1 x n + 1。
text1和text2只取前i個和前j個字的時候的最長共同子字串。i = 0或是j = 0都填入0。text[i] == text[j]時,此時填入dp[i - 1][j - 1] + 1。
dp[i - 1][j]和dp[i][j - 1]較大者。
text1或是text2的最後一個字,取較長者。dp[m][n]就是答案了。
m個和前n個就是原來的兩個字串。LeetCode C++