1143.Longest Common Subsequence
Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.
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.
For example, "ace" is a subsequence of "abcde". A common subsequence of two strings is a subsequence that is common to both strings.
翻譯
有兩個字串 text1, text2,求最長共同子序列的長度。
子序列的意思是一個字串 B 是根據另一個字串 A 產生的,不改變順序的情況下,取 A 中的某些字元組成新的字串 B。
舉例來說, "ace" 就是 "abcde" 的其中一個子序列,共同子序列就是兩個字串同時都擁有的子序列。
Example:
Example2:
思路
一、極限值/特殊狀況
若沒有共同子序列則回傳 0
二、哪種資料結構解
dynamic programmer 動態規劃
三、大概會怎麼解
若不懂動態規劃請先去了解,可參考這兩篇文章:
根據動態規劃的規則,逐字比較。
用二維陣列儲存比較後的結果。
型別
解題
Last updated