★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-mwyrwldt-ks.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given two strings text1
and text2
, return the length of their longest common subsequence.git
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.github
If there is no common subsequence, return 0.微信
Example 1:spa
Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3.
Example 2:code
Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3.
Example 3:htm
Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.
Constraints:blog
1 <= text1.length <= 1000
1 <= text2.length <= 1000
給定兩個字符串text1和text2,返回它們最長公共子序列的長度。rem
字符串的子序列是從原始字符串生成的新字符串,刪除了一些字符(不能爲無),而不更改其他字符的相對順序。(例如,「ace」是「abcde」的子序列,而「aec」不是)。兩個字符串的公共子序列是兩個字符串的公共子序列。字符串
若是沒有公共子序列,則返回0。
例1:
輸入:text1=「abcde」,text2=「ace」
輸出:3
說明:最長的常見子序列是「ace」,其長度爲3。
例2:
輸入:text1=「abc」,text2=「abc」
輸出:3
說明:最長的常見子序列爲「abc」,其長度爲3。
例3:
輸入:text1=「abc」,text2=「def」
輸出:0
說明:沒有這種常見的子序列,因此結果是0。
限制條件:
76ms
1 sample 76 ms submission 2 class Solution { 3 func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int { 4 var chars1 = Array(text1) 5 var chars2 = Array(text2) 6 var memo = [[Int]](repeating:[Int](repeating: 0, count:chars2.count+1), count: chars1.count+1) 7 8 for i in chars1.indices { 9 for j in chars2.indices { 10 11 if chars1[i] == chars2[j] { 12 memo[i+1][j+1] = memo[i][j] + 1 13 } else { 14 memo[i+1][j+1] = max(memo[i][j+1], memo[i+1][j]) 15 } 16 } 17 } 18 return memo[chars1.count][chars2.count] 19 } 20 }
1 class Solution { 2 func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int { 3 var outer = Array(text1) // outer 4 var inner = Array(text2) // inner 5 var T = Array(repeating: Array(repeating: 0, count: outer.count+1), count: inner.count+1) 6 7 for j in 1...inner.count { 8 for i in 1...outer.count { 9 if inner[j-1] == outer[i-1] { 10 T[j][i] = T[j-1][i-1]+1 11 } else { 12 T[j][i] = max(T[j][i-1], T[j-1][i]) 13 } 14 } 15 } 16 return T[inner.count][outer.count] 17 } 18 }
84ms
1 class Solution { 2 func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int { 3 return solu(text1: text1, text2: text2) 4 } 5 6 private func solu(text1: String, text2: String) -> Int { 7 var dp: [[Int]] = Array(repeating: Array(repeating: 0, count: text2.count + 1), count: text1.count + 1) 8 9 let t1 = text1.map { String($0) } 10 let t2 = text2.map { String($0) } 11 for i in 0 ..< t1.count { 12 for j in 0 ..< t2.count { 13 if t1[i] == t2[j] { 14 dp[i + 1][j + 1] = 1 + dp[i][j] 15 } else { 16 dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]) 17 } 18 } 19 } 20 return dp[text1.count][text2.count] 21 } 22 23 private func find(text1: String, text2: String, dp: inout [String: Int]) -> Int { 24 guard text1 != text2 else { 25 return text1.count 26 } 27 guard text1.count > 0 && text2.count > 0 else { return 0 } 28 29 let key = text1 + "|" + text2 30 31 if let cache = dp[key] { 32 return cache 33 } 34 35 var result = 0 36 for i in 0 ..< text1.count { 37 for j in 0 ..< text2.count { 38 var t1 = text1 39 var t2 = text2 40 41 let i1 = text1.index(text1.startIndex, offsetBy: i) 42 t1.remove(at: i1) 43 44 let j1 = text2.index(text2.startIndex, offsetBy: j) 45 t2.remove(at: j1) 46 47 result = max( 48 find(text1: text1, text2: t2, dp: &dp), 49 find(text1: t1, text2: text2, dp: &dp), 50 result 51 ) 52 } 53 } 54 dp[key] = result 55 return result 56 } 57 }
88ms
1 class Solution { 2 func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int { 3 var dp = [[Int]](repeating: [Int](repeating: 0, count: text2.count + 1), count: text1.count + 1) 4 let text1 = Array(text1) 5 let text2 = Array(text2) 6 for i in 1..<text1.count + 1 { 7 for j in 1..<text2.count + 1 { 8 if text1[i-1] == text2[j-1] { 9 dp[i][j] = 1 + dp[i-1][j-1] 10 } else { 11 dp[i][j] = max(dp[i-1][j], dp[i][j-1]) 12 } 13 } 14 } 15 return dp.last!.last! 16 } 17 }
Runtime: 92 ms
1 class Solution { 2 func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int { 3 let m:Int = text1.count 4 let n:Int = text2.count 5 let arr1:[Character] = Array(text1) 6 let arr2:[Character] = Array(text2) 7 var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:n + 1),count:m + 1) 8 for i in 1...m 9 { 10 for j in 1...n 11 { 12 if arr1[i - 1] == arr2[j - 1] 13 { 14 dp[i][j] = dp[i - 1][j - 1] + 1 15 } 16 else 17 { 18 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) 19 } 20 } 21 } 22 return dp[m][n] 23 } 24 }