LeetCode - #72 Edit Distance (Top 100)

Get into the habit of writing together! This is the 18th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

foreword

This question is the top 100 high frequency questions of LeetCode

Our community will successively organize Gu Yi ( Netflix growth hacker, author of "The Way of the iOS Interview", ACE professional fitness coach. )'s Swift algorithm problem solutions into a text version for everyone to learn and read.

We have updated 71 issues of LeetCode algorithm so far, and we will keep the update time and progress ( released at 9:00 am on Monday, Wednesday, and Friday ). There will be a big improvement.

If you don't accumulate a small step, you can't go a thousand miles; if you don't accumulate a small stream, you can't make a river. The Swift community accompanies you to move forward. If you have suggestions and comments, please leave a message at the end of the article, we will try our best to meet your needs.

Difficulty Level: Difficult

1. Description

Given two words word1and , please return the minimum number of operands used word2to word1convert to .word2

You can perform the following three operations on a word:

  • insert a character
  • delete a character
  • replace a character

2. Example

Example 1

输入:word1 = "horse", word2 = "ros"
输出:3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')
复制代码

Example 2

输入:word1 = "intention", word2 = "execution"
输出:5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u')
复制代码

Restrictions:

  • 0 <= word1.length, word2.length <= 500
  • word1and word2consists of lowercase English letters

3. Answers

class EditDistance {
    func minDistance(word1: String, _ word2: String) -> Int {
        let aChars = [Character](word1.characters)
        let bChars = [Character](word2.characters)
        let aLen = aChars.count
        let bLen = bChars.count
        
        var dp = Array(count: aLen + 1, repeatedValue:(Array(count: bLen + 1, repeatedValue: 0)))
        
        for i in 0...aLen {
            for j in 0...bLen {
                if i == 0 {
                    dp[i][j] = j
                } else if j == 0 {
                    dp[i][j] = i
                } else if aChars[i - 1] == bChars[j - 1] {
                    dp[i][j] = dp[i - 1][j - 1]
                } else {
                    dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1
                }
            }
        }
        
        return dp[aLen][bLen]
    }
}
复制代码
  • Main idea: 2D dynamic programming, find the smallest steps from inserting, deleting or replacing a character.
  • Time Complexity: O(mn)
  • Space Complexity: O(mn)

Repository for the algorithm solution: LeetCode-Swift

Click to go to LeetCode practice

about us

We are jointly maintained by Swift enthusiasts. We will share the technical content centered on Swift combat, SwiftUI, and Swift foundation, and also organize and collect excellent learning materials.

Guess you like

Origin juejin.im/post/7089012153604128781