为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode1143. 最长公共子序列 | Longest Common Subsequence

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11297774.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given two strings text1 and text2, return the length of their longest common subsequence.

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.

Example 1:

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

Example 2:

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

Example 3:

Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.

Constraints:

  • 1 <= text1.length <= 1000
  • 1 <= text2.length <= 1000
  • The input strings consist of lowercase English characters only.

给定两个字符串text1和text2,返回它们最长公共子序列的长度。

字符串的子序列是从原始字符串生成的新字符串,删除了一些字符(不能为无),而不更改其余字符的相对顺序。(例如,“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。

限制条件:

  • 1<=text1.长度<=1000
  • 1<=text2.长度<=1000
  • 输入字符串仅由小写英文字符组成。

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 }

76ms
 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

Memory Usage: 26.6 MB
 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 }

  

posted @ 2019-08-04 12:01  为敢技术  阅读(684)  评论(0编辑  收藏  举报