[Swift]LeetCode583. 两个字符串的删除操作 | Delete Operation for Two Strings

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 }

原文地址:https://www.cnblogs.com/strengthen/p/10420808.html

时间: 2025-01-11 10:38:25

[Swift]LeetCode583. 两个字符串的删除操作 | Delete Operation for Two Strings的相关文章

[LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作

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 ne

leetcode 583. 两个字符串的删除操作

给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符. 示例 1: 输入: "sea", "eat" 输出: 2 解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea" 说明: 给定单词的长度不超过500. 给定单词中的字符只含有小写字母. python解法 动态规划 与两个字符串的最小

[LeetCode] Delete Operation for Two Strings 两个字符串的删除操作

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 ne

C++中对字符串进行插入、替换、删除操作

#include <iostream> #include <string> using std::cout; using std::endl; using std::string; int main(void){ string str1="We can insert a string"; string str2="a str into "; //在字符串指定位置前面插入指定字符串 cout <<str1.insert(14,str

C++(十)— 字符串进行插入、替换、删除操作

 1.C++中对字符串进行插入.替换.删除操作 #include<iostream> #include<algorithm> #include<stdio.h> #include <vector> #include<string> using namespace std; int main() { string s = "123456"; // 在字符串指定位置前插入字符, cout << s.insert(2,

Q712 两个字符串的最小ASCII删除和

给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的ASCII值的最小和. 示例 1: 输入: s1 = "sea", s2 = "eat" 输出: 231 解释: 在 "sea" 中删除 "s" 并将 "s" 的值(115)加入总和. 在 "eat" 中删除 "t" 并将 116 加入总和. 结束时,两个字符串相等,115 + 116 = 231 就是符合

String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。

// 在字符串中删除指定字符串. String phoneNum="1795112345"; phoneNum = phoneNum.replace("17951", ""); System.out.println(phoneNum); //判断指定字符串是否包含另一字符串 String phoneNum="1795112345"; String IpNum="17951"; return phoneNum

输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。

在字符串中删除特定的字符 输入”They are students.” 和”aeiou”则删除之后第一个字符串变成 Thy r stdnts. 本题还用到了 “字符串hash” #include <iostream> #include <cstring> char * string_del_characters( char * const src, const char * const dest ) { int destLen = strlen( dest ); int hash_

Swift和Java关于字符串和字符的比较

1. 字符串字面量 Swift和Java关于字符串操作大同小异. Swift:字符串字面量(String Literals):我们可以在代码中包含一段预定义的字符串值作为字符串字面量. 字符串字面量是由双引号 ("") 包裹着的具有固定顺序的文本字符集.字符串字面量可以用于为常量和变量提供初始值. 代码如下: let someString = "Some string literal value" 字符串字面量可以包含以下特殊字符: 转义字符\0(空字符).\\(反