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:

Input: text1 = "abcde", text2 = "ace" 
Output: 3  
Explanation: The longest common subsequence is "ace" and its length is 3.

Example2:

Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.

思路

一、極限值/特殊狀況

  • 若沒有共同子序列則回傳 0

二、哪種資料結構解

dynamic programmer 動態規劃

三、大概會怎麼解

型別

解題

Last updated