Skip to content

Commit

Permalink
add js solution for minDistance
Browse files Browse the repository at this point in the history
  • Loading branch information
jackeyjia authored Jul 17, 2021
1 parent 63b3ede commit a24ca64
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions problems/0583.两个字符串的删除操作.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ class Solution:
Go:


Javascript:
```javascript
const minDistance = (word1, word2) => {
let dp = Array.from(Array(word1.length + 1), () => Array(word2.length+1).fill(0));

for(let i = 1; i <= word1.length; i++) {
dp[i][0] = i;
}

for(let j = 1; j <= word2.length; j++) {
dp[0][j] = j;
}

for(let i = 1; i <= word1.length; i++) {
for(let j = 1; j <= word2.length; j++) {
if(word1[i-1] === word2[j-1]) {
dp[i][j] = dp[i-1][j-1];
} else {
dp[i][j] = Math.min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + 2);
}
}
}

return dp[word1.length][word2.length];
};
```


-----------------------
Expand Down

0 comments on commit a24ca64

Please sign in to comment.