【leetcode】72. Edit Distance【java】
3768 단어 leetcode
You have the following 3 operations permitted on a word:
a) Insert a character b) Delete a character c) Replace a character
해석 하 다. 동적 계획
This is a classic problem of Dynamic Programming. We define the state
dp[i][j]
to be the minimum number of operations to convert word1[0..i - 1]
to word2[0..j - 1]
. The state equations have two cases: the boundary case and the general case. Note that in the above notations, both i
and j
take values starting from 1
. For the boundary case, that is, to convert a string to an empty string, it is easy to see that the mininum number of operations to convert
word1[0..i - 1]
to ""
requires at least i
operations (deletions). In fact, the boundary case is simply: dp[i][0] = i
; dp[0][j] = j
. Now let's move on to the general case, that is, convert a non-empty
word1[0..i - 1]
to another non-empty word2[0..j - 1]
. Well, let's try to break this problem down into smaller problems (sub-problems). Suppose we have already known how to convert word1[0..i - 2]
to word2[0..j - 2]
, which is dp[i - 1][j - 1]
. Now let's consider word[i - 1]
and word2[j - 1]
. If they are euqal, then no more operation is needed and dp[i][j] = dp[i - 1][j - 1]
. Well, what if they are not equal? If they are not equal, we need to consider three cases:
word1[i - 1]
by word2[j - 1]
( dp[i][j] = dp[i - 1][j - 1] + 1 (for replacement)
); word1[i - 1]
and word1[0..i - 2] = word2[0..j - 1]
( dp[i][j] = dp[i - 1][j] + 1 (for deletion)
); word2[j - 1]
to word1[0..i - 1]
and word1[0..i - 1] + word2[j - 1] = word2[0..j - 1]
( dp[i][j] = dp[i][j - 1] + 1 (for insertion)
). Make sure you understand the subtle differences between the equations for deletion and insertion. For deletion, we are actually converting
word1[0..i - 2]
to word2[0..j - 1]
, which costs dp[i - 1][j]
, and then deleting the word1[i - 1]
, which costs 1
. The case is similar for insertion. Putting these together, we now have:
dp[i][0] = i
; dp[0][j] = j
; dp[i][j] = dp[i - 1][j - 1]
, if word1[i - 1] = word2[j - 1]
; dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i - 1][j] + 1, dp[i][j - 1] + 1)
, otherwise. public class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 1; i < m + 1; i++){
dp[i][0] = i;
}
for (int j = 1; j < n + 1; j++){
dp[0][j] = j;
}
for (int i = 1; i < m + 1; i++){
for (int j = 1; j < n + 1; j++){
if (word1.charAt(i - 1) == word2.charAt(j - 1)){
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j],dp[i][j - 1]));
}
}
}
return dp[m][n];
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
0부터 시작하는 LeetCode Day8 「1302. Deepest Leaves Sum」해외에서는 엔지니어의 면접에 있어서 코딩 테스트라고 하는 것이 행해지는 것 같고, 많은 경우, 특정의 함수나 클래스를 주제에 따라 실장한다고 하는 것이 메인이다. 빠른 이야기가 본고장에서도 행해지고 있는 것 같은 코...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.