LeetCode-72.編輯距離(Edit Distance)

2021-02-20 極客算法

72. 編輯距離

給你兩個單詞 word1 和 word2,請你計算出將 word1 轉換成 word2 所使用的最少操作數 。

你可以對一個單詞進行如下三種操作:

1.插入一個字符2.刪除一個字符3.替換一個字符

示例 1:

輸入:word1 = "horse", word2 = "ros"輸出:3解釋:horse -> rorse (將 'h' 替換為 'r')rorse -> rose (刪除 'r')rose -> ros (刪除 'e')

示例 2:

輸入:word1 = "intention", word2 = "execution"輸出:5解釋:intention -> inention (刪除 't')inention -> enention (將 'i' 替換為 'e')enention -> exention (將 'n' 替換為 'x')exention -> exection (將 'n' 替換為 'c')exection -> execution (插入 'u')

來源:力扣(LeetCode)

連結:https://leetcode-cn.com/problems/edit-distance

Link:https://leetcode.com/problems/edit-distance/

動態規劃

O(M*N)

    ''    R    O    S    # word2
'' 0 1 2 3
H 1 > 插入 | \O 2 | \ | \ R 3 | \ | \ S 4 | \ | \ 替換E 5 v 刪除 45.Degree
# word1

遞推公式

定義table[i][j]代表word1[:i]與word2[:j]能夠匹配的時候需要的操作次數(包含i, j)

# 如果兩個單詞字符word1[i] == word2[j]:table[i][j] = table[i - 1][j - 1]
# 如果不想等, 三個策略選擇次數最少的一個方案:
# 策略1, word1插入一個字符
table[i][j] = table[i][j - 1] + 1
# 策略2, word1刪除一個字符
table[i][j] = table[i - 1][j] + 1
# 策略3, word1替換一個字符
table[i][j] = table[i - 1][j - 1]

計算方向

水平逐行掃描

邊界條件
# 水平第一行, 代表空字符的word1, 與word2相比配, 只能依次插入
# 水平第一行, 代表word1, 與空字符的word2相比配, 只能依次刪除

初始條件
table[0][0] = 0 # 表示兩個空字符,無需任何操作

代碼如下:

class Solution:    def minDistance(self, word1: str, word2: str) -> int:
table = [[0 for j in range(len(word2) + 1)] for i in range(len(word1) + 1)]
for j in range(1, len(table[0])): table[0][j] = table[0][j - 1] + 1
for i in range(1, len(table)): table[i][0] = table[i - 1][0] + 1
for i in range(len(word1)): for j in range(len(word2)):
if word1[i] == word2[j]: table[i + 1][j + 1] = table[i][j] else: table[i + 1][j + 1] = min(table[i + 1][j], table[i][j + 1], table[i][j]) + 1
return table[-1][-1]

手動遞推
    ''    R    O    S    # word2
'' 0 1 2 3
H 1 1 2 3
O 2 2 1 2
R 3 2 2 2
S 4 3 3 2
E 5 4 4 3
# word1

--End--

相關焦點

  • ​LeetCode刷題實戰72:編輯距離
    今天和大家聊的問題叫做 編輯距離,我們先來看題面:https://leetcode-cn.com/problems/edit-distanceGiven two words word1 and word2, find the minimum number of operations required to convert word1
  • ​LeetCode刷題實戰161:相隔為1的編輯距離
    今天和大家聊的問題叫做 相隔為 1 的編輯距離  ,我們先來看題面:https://leetcode-cn.com/problems/one-edit-distance/Given two strings S and T, determine if they are both one edit distance apart.
  • Leetcode題解 Edit Distance
  • [LeetCode] 973. K Closest Points to Origin 最接近原點的K個點
    (Here, the distance between two points on a plane is the Euclidean distance.)You may return the answer in any order.
  • Cool Edit Pro
    你能使用它記錄你的自己的音樂,聲音或另外的聲音,編輯它,與另外的聲音或音樂的部分混合它,象Reverb,合唱團,並且迴響一樣增加效果到它,equalize它,並且主人它以便你能燒它到CD ,在全球資訊網上郵寄它,或發電子郵件給它。一旦你開始,你將在你能完成的被驚奇!本站提供cool edit pro 2.1中文版下載。
  • Keep your distance? 保持距離
    distance, in a good way.By ordering the commoner to keep his distance, the gentleman is telling the latter to stop acting in any way that may suggest they're familiar or close or friendly.
  • LeetCode刷題:Array系列之Remove Element
    for (int i = 0; i < count; ++i)        cout << "vec[" << i << "] = " << vec[i] << endl;    return 0;}2.4 運行結果3、第二種實現方式思路:利用remove和distance
  • 雙語新聞:The furthest distance in the world(世界上最遙遠的距離)
    新東方網>英語>英語學習>英語閱讀>雙語新聞>正文雙語新聞:The furthest distance in the world(世界上最遙遠的距離) 2007-02-25 08:56 來源:中國日報網站
  • 您知道edit是什麼意思嗎?
    說到edit這個單詞,很多人都知道指的是編輯、校訂,既可以做動詞,又可以做名詞。今天,我們就一起看一下edit的用法。首先,我們看一下edit做動詞的用法。這句話中edited是edit的過去分詞,意思是編輯、編纂、校訂(文章、書籍等)。2、He's editing a book of essays by Isaiah Berlin.他正在編輯一本艾賽亞伯林的散文集。這句話中editing是edit的現在分詞,意思是編選、編纂、編集。
  • 保持你的距離(Keep your distance)
    最早看到英文版的「保持你的距離」(Keep your distance),還是年少讀魯迅的時候。他在《一點比喻》裡,引用了叔本華舉過的一個例子:有一群豪豬,在冬天想用了大家的體溫來禦寒冷,緊靠起來了,但它們彼此即刻又覺得刺的疼痛,於是乎又離開。然而溫暖的必要,再使它們靠近時,卻又吃了照樣的苦。
  • 權力距離指數-Power distance index
    權力距離又是什麼意思呢?霍夫斯泰德的定義是:權力距離指數是社會中權力低的人對權力分配不均接納和預期的程度。更迷惑了嗎?因為初初看來,這個概念應該指的是社會不平等的狀態,很多人引用這個文化維度也是這麼解釋的,我起初也是這麼理解的。仔細看了霍氏的書,才明白這個距離指的是情感距離(emotion distance)。
  • LeetCode刷題第三周【數組(簡單)】
    1.1 解題思路我們通過示例發現,數組的下標 i 與 arr[i] 的差值 distance 就是缺失的正整數個數。因此我們可以通過判斷 distance 和 k 的大小,來知道我們要找的數所在的區間。我們同樣也可以通過 distance 計算出,我們需要的值。
  • 英語情景口語[88]:distance and speed距離與速度
    088 distance and speed   Words   Near/close far rush hurry crawl pace velocity wide tall long short length deep narrow broad high depth shallow short-cut
  • Leetcode刷題-動態規劃
    本文對leetcode上部分動態規劃問題進行分析並用python實現5. 最長回文子串 題目描述給定一個字符串 s,找到 s 中最長的回文子串。你可以假設 s 的最大長度為 1000。編輯距離 題目描述:給你兩個單詞 word1 和 word2,請你計算出將 word1 轉換成 word2 所使用的最少操作數 。
  • leetcode C++11題解系列-071 編輯距離
    ///* * * 編輯距離 給你兩個單詞 word1 和 word2,請你計算出將 word1 轉換成 word2 所使用的最少操作數 。
  • [LeetCode] Exam Room 考試房間
    When a student enters the room, they must sit in the seat that maximizes the distance to the closest person.  If there are multiple such seats, they sit in the seat with the lowest number.
  • Go 實現 LeetCode 全集
    Water - https://leetcode.com/problems/container-with-most-water/Binary[X] Sum of Two Integers - https://leetcode.com/problems/sum-of-two-integers/[X] Number of 1 Bits - https://leetcode.com
  • LeetCode-4-動態規劃
    return n dp=(n+1)*[0] # 注意是n+1 dp[1]=1 dp[2]=2 for i in range(3,n+1): dp[i]=dp[i-1]+dp[i-2] return dp[n] # 返回最後一個數72
  • 歐氏距離、閔氏距離和馬氏距離(閔可夫斯基軼事外一則)
    本文介紹一下另外兩種常用的距離定義:閔可夫斯基(Minkowski)[1]距離(簡稱閔氏距離)和馬哈拉諾比斯[2](Mahalanobis)距離(簡稱馬氏距離),它們都可以看作是歐氏距離的推廣。Matlab中計算距離的函數為:pdist()   D = pdist(X)D = pdist(X,distance)Description    D= pdist(X)    計算 X 中各對行向量的相互距離(X是一個m-by-n的矩陣).
  • 每日經典語錄:Eternity is not a distance but a decision
    新東方網>英語>英語學習>口語>每日一句英語>正文每日經典語錄:Eternity is not a distance but a decision 2013-01-28 16:20 來源:恆星英語 作者: