1. 程式人生 > >[Swift]LeetCode583. 兩個字符串的刪除操作 | Delete Operation for Two Strings

[Swift]LeetCode583. 兩個字符串的刪除操作 | Delete Operation for Two Strings

seq var delete 索引 com imu char star init

Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

Example 1:

Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea". 

Note:

  1. The length of given words won‘t exceed 500.
  2. Characters in given words can only be lower-case letters.

給定兩個單詞 word1 word2,找到使得 word1 word2 相同所需的最小步數,每步可以刪除任意一個字符串中的一個字符。

示例 1:

輸入: "sea", "eat"
輸出: 2
解釋: 第一步將"sea"變為"ea",第二步將"eat"變為"ea"

說明:

  1. 給定單詞的長度不超過500。
  2. 給定單詞中的字符只含有小寫字母。

124ms

 1 class Solution {
 2     func minDistance(_ word1: String, _ word2: String) -> Int {
 3         if word1.count < word2.count {
 4             return minDistance(word2, word1)
 5         }
 6         var cache = Array(repeating: Array(repeating: -1, count: word2.count), count: word1.count)
7 return word1.count + word2.count - lcs(Array(word1), Array(word2), word1.count - 1, word2.count - 1, &cache)*2 8 } 9 10 func lcs(_ word1:[Character], _ word2:[Character], _ i:Int, _ j:Int, _ cache:inout [[Int]]) -> Int { 11 var maxLenght = 0 12 if i < 0 || j < 0 { 13 return maxLenght 14 } 15 if cache[i][j] != -1 { 16 return cache[i][j] 17 } 18 if word1[i] == word2[j] { 19 maxLenght = max(maxLenght, lcs(word1, word2, i - 1, j - 1, &cache) + 1) 20 } else { 21 maxLenght = max(maxLenght, lcs(word1, word2, i, j - 1, &cache), lcs(word1, word2, i - 1, j, &cache)) 22 } 23 cache[i][j] = maxLenght 24 return maxLenght 25 } 26 }

132ms

 1 class Solution {
 2     func minDistance(_ word1: String, _ word2: String) -> Int {
 3         if word1.count == 0 {
 4             return word2.count
 5         }
 6         if word2.count == 0 {
 7             return word1.count
 8         }
 9         let len = longestCommonSequence(Array(word1), Array(word2))
10         return word1.count + word2.count - 2 * len
11     }
12     
13     func longestCommonSequence(_ string1: [Character], _ string2: [Character]) -> Int {
14         let m = string1.count
15         let n = string2.count
16         var dp = [[Int]]()
17         for _ in 0 ..< m + 1 {
18             let array = [Int].init(repeating: 0, count: n + 1)
19             dp.append(array)
20         }
21         
22         for i in 0 ..< m {
23             for j in 0 ..< n {
24                 if string1[i] == string2[j] {
25                     dp[i+1][j+1] = dp[i][j] + 1
26                 } else {
27                     dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])
28                 }
29             }
30         }
31         return dp[m][n]
32     }
33 }

136ms

 1 class Solution {
 2     func minDistance(_ word1: String, _ word2: String) -> Int {
 3     var word1 = Array(word1)
 4     var word2 = Array(word2)
 5     var dp = [[Int]](repeating: [Int](repeating: 0, count: word2.count + 1), count: word1.count + 1)
 6     
 7     for row in 0..<dp.count{
 8         dp[row][0] = row
 9     }
10     
11     for column in 0..<dp[0].count{
12         dp[0][column] = column
13     }
14     
15     for row in 1..<dp.count{
16         for column in 1..<dp[row].count{
17             if word1[row-1] == word2[column-1]{
18                 dp[row][column] = dp[row - 1][column - 1]
19             }else{
20                 dp[row][column] = min(dp[row - 1][column], dp[row][column - 1]) + 1
21             }
22         }
23     }
24     
25     return dp.last!.last!
26   }
27 }

156ms

 1 class Solution {
 2     func minDistance(_ word1: String, _ word2: String) -> Int {
 3         let word1 = Array(word1)
 4         let word2 = Array(word2)
 5         let len1 = word1.count
 6         let len2 = word2.count
 7         guard len1 != 0 else { return len2 }
 8         guard len2 != 0 else { return len1 }
 9         
10         var dp: [[Int]] = Array(repeating: Array(repeating: 0, count: len2 + 1), count: len1 + 1)
11         for i in 0 ... len1 {
12             dp[i][0] = i
13         }
14         
15         for i in 0 ... len2 {
16             dp[0][i] = i
17         }
18         
19         for i in 1 ... len1 {
20             for j in 1 ... len2 {
21                 if word1[i - 1] == word2[j - 1] {
22                     dp[i][j] = dp[i - 1][j - 1]
23                 } else {
24                     dp[i][j] = min(dp[i - 1][j - 1] + 2, min(dp[i][j - 1] + 1, dp[i - 1][j] + 1))
25                 }
26             }
27         }
28         
29         return dp[len1][len2]
30     }
31 }

Runtime: 732 ms Memory Usage: 20.4 MB
 1 class Solution {
 2     func minDistance(_ word1: String, _ word2: String) -> Int {
 3         var n1:Int = word1.count
 4         var n2:Int = word2.count
 5         var memo:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:n2 + 1),count:n1 + 1)
 6         return helper(word1, word2, 0, 0, &memo)
 7     }
 8     
 9     func helper(_ word1: String, _ word2: String,_ p1:Int,_ p2:Int,_ memo:inout [[Int]]) -> Int
10     {
11         if memo[p1][p2] != 0
12         {
13             return memo[p1][p2]
14         }
15         var n1:Int = word1.count
16         var n2:Int = word2.count
17         if p1 == n1 || p2 == n2
18         {
19             return n1 - p1 + n2 - p2            
20         }
21         if word1[p1] == word2[p2]
22         {
23             memo[p1][p2] = helper(word1, word2, p1 + 1, p2 + 1, &memo)
24         }
25         else
26         {
27              memo[p1][p2] = 1 + min(helper(word1, word2, p1 + 1, p2, &memo), helper(word1, word2, p1, p2 + 1, &memo))
28         }
29         return memo[p1][p2]
30     }
31 }
32 
33 extension String {        
34     //subscript函數可以檢索數組中的值
35     //直接按照索引方式截取指定索引的字符
36     subscript (_ i: Int) -> Character {
37         //讀取字符
38         get {return self[index(startIndex, offsetBy: i)]}
39     }
40 }

[Swift]LeetCode583. 兩個字符串的刪除操作 | Delete Operation for Two Strings