Skip to content

Commit 40cd0c2

Browse files
Merge pull request youngyangyang04#496 from jackeyjia/patch-16
add js solution for minDistance
2 parents e70378f + a24ca64 commit 40cd0c2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

problems/0583.两个字符串的删除操作.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,32 @@ class Solution:
149149
Go:
150150

151151

152+
Javascript:
153+
```javascript
154+
const minDistance = (word1, word2) => {
155+
let dp = Array.from(Array(word1.length + 1), () => Array(word2.length+1).fill(0));
156+
157+
for(let i = 1; i <= word1.length; i++) {
158+
dp[i][0] = i;
159+
}
160+
161+
for(let j = 1; j <= word2.length; j++) {
162+
dp[0][j] = j;
163+
}
164+
165+
for(let i = 1; i <= word1.length; i++) {
166+
for(let j = 1; j <= word2.length; j++) {
167+
if(word1[i-1] === word2[j-1]) {
168+
dp[i][j] = dp[i-1][j-1];
169+
} else {
170+
dp[i][j] = Math.min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + 2);
171+
}
172+
}
173+
}
174+
175+
return dp[word1.length][word2.length];
176+
};
177+
```
152178

153179

154180
-----------------------

0 commit comments

Comments
 (0)