1. 程式人生 > >[Leetcode] Edit Distance

[Leetcode] Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character
b) Delete a character
c) Replace a character

自然語言處理(NLP)中,有一個基本問題就是求兩個字串的minimal Edit Distance

, 也稱Levenshtein distance。受到一篇Edit Distance介紹文章的啟發,本文用動態規劃求取了兩個字串之間的minimal Edit Distance. 動態規劃方程將在下文進行講解。 

1. what is minimal edit distance?

簡單地說,就是僅通過插入(insert)、刪除(delete)和替換(substitute)個操作將一個字串s1變換到另一個字串s2的最少步驟數。熟悉演算法的同學很容易知道這是個動態規劃問題。 

其實一個替換操作可以相當於一個delete+一個insert,所以我們將權值定義如下:

I  (insert):1

D (delete):1

S (substitute):2

2. example:

intention->execution

Minimal edit distance:

delete i ; n->e ; t->x ; insert c ; n->u 求和得cost=8

3.calculate minimal edit distance dynamically
思路見註釋,這裡D[i,j]就是取s1前i個character和s2前j個character所得minimal edit distance

三個操作動態進行更新:

D(i,j)=min { D(i-1, j) +1, D(i, j-1) +1 , D(i-1, j-1) + s1[i]==s2[j] ? 0 : 2};中的三項分別對應D,I,S。(詳見

我同學的部落格

 1 class Solution {
 2 public:
 3     int minDistance(string word1, string word2) {
 4         int len1 = word1.length();
 5         int len2 = word2.length();
 6         if (len1 == 0) return len2;
 7         if (len2 == 0) return len1;
 8         vector<vector<int> > dp(len1 + 1, vector<int>(len2 + 1));
 9         for (int i = 0; i <= len1; ++i) dp[i][0] = i;
10         for (int j = 0; j <= len2; ++j) dp[0][j] = j;
11         int cost;
12         for (int i = 1; i <= len1; ++i) {
13             for (int j = 1; j <= len2; ++j) {
14                 cost = (word1[i-1] == word2[j - 1]) ? 0 : 1;
15                 dp[i][j] = min(dp[i-1][j-1] + cost, min(dp[i][j-1] + 1, dp[i-1][j] + 1));
16             }
17         }
18         return dp[len1][len2];
19     }
20 };

相關推薦

leetcode - Edit Distance

public margin ack leetcode rgb perm win ati line Given two words word1 and word2, find the minimum number of steps required to conv

LeetCode-Edit Distance 編輯距離與動態規劃

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have th

[Leetcode] Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the

LeetCode--Edit Distance(字串編輯距離)Python

題目: 給定兩個字串。計算這兩個字串的編輯距離。可編輯方式包含3種:插入、刪除、替換。 解題思路: 考慮使用動態規劃來解題。用output[i][j]來儲存word1[0:i]和word2[0:j]的編輯距離。則output[i][j]可以由output[i-1][j],o

LeetCode | Edit Distance(字串編輯距離)

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1

Leetcode: Edit Distance 編輯距離

歡迎討論~ Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following

第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP

blog http else true rep 猜想 col () 字符串 Leetcode72 看起來比較棘手的一道題(列DP方程還是要大膽猜想。。) DP方程該怎麽列呢? dp[i][j]表示字符串a[0....i-1]轉化為b[0....j-1]的最少距離 轉移方程分

leetcode 72. Edit Distance

沒有 require targe 了吧 length rip follow des steps link Given two words word1 and word2, find the minimum number of steps required to conv

Leetcode 161: One Edit Distance

nbsp one determine spa ++ solution math ive ngs Given two strings S and T, determine if they are both one edit distance apart. 1 publ

[leetcode]161. One Edit Distance編輯步數為一 [leetcode]72. Edit Distance 最少編輯步數

Given two strings s and t, determine if they are both one edit distance apart. Note:  There are 3 possiblities to satisify one edit d

Leetcode】【DP】 72. Edit Distance / 編輯距離

Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have th

LeetCode】128.Edit Distance

題目描述(Hard) Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.

leetcode】72. (Hard) Edit Distance

題目連結 解題思路: DP 提交程式碼: class Solution { public int minDistance(String word1, String word2) { int[][] dp=new int[word2.length(

LeetCode 72-Edit Distance(動態規劃)

本題為經典的動態規劃。解決本題的前提是把這題的動態方程先搞明白。 題目:(動態規劃)https://leetcode.com/problems/edit-distance/ 概述:給定兩個單詞,給了三種方法(替換,刪除,插入),用這三種方法使得兩個單詞相同。 思路:採用動態規

LeetCode 72.Edit Distance (編輯距離)

題目: 給定兩個單詞 word1 和 word2,計算出將 word1 轉換成 word2 所使用的最少運算元 。 你可以對一個單詞進行如下三種操作: 插入一個字元 刪除一個字元 替換一個字

[Leetcode 72]編輯距離 Edit Distance

【題目】 Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have th

LeetCode】161. One Edit Distance

Difficulty: Medium  More:【目錄】LeetCode Java實現 Description Given two strings S and T, determine if they are both one edit distance apart. Intuition

[leetcode]72. Edit Distance

沒想出來。不是dp難,是不會抽象這個關係 class Solution { public int minDistance(String word1, String word2) { int n = word1.length(); int m = word2.lengt

LeetCode打卡之 Edit Distance

題目 Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the followin

leetCode 72.Edit Distance (編輯距離) 解題思路和方法

Edit Distance Given two words word1 and word2, find the minimum number of steps required to conve