Title: #72 - Edit Distance - LeetCode JavaScript Solutions
Open Graph Title: #72 - Edit Distance - LeetCode JavaScript Solutions
X Title: #72 - Edit Distance - LeetCode JavaScript Solutions
Description: Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three ...
Open Graph Description: Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three ...
X Description: Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three ...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/edit-distance
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/edit-distance","name":"#72 - Edit Distance - LeetCode JavaScript Solutions","description":"Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three ...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {string} word1\n * @param {string} word2\n * @return {number}\n */\nvar minDistance = function(word1, word2) {\n const cache = new Array(word1.length + 1).fill(0).map(() => new Array(word2.length + 1));\n\n for (let i = 0; i <= word1.length; i++) {\n for (let j = 0; j <= word2.length; j++) {\n if (i === 0) {\n cache[0][j] = j;\n } else if (j === 0) {\n cache[i][0] = i;\n } else if (word1[i - 1] == word2[j - 1]) {\n cache[i][j] = cache[i - 1][j - 1];\n } else {\n cache[i][j] = Math.min(cache[i][j - 1], cache[i - 1][j - 1], cache[i - 1][j]) + 1;\n }\n }\n }\n\n return cache[word1.length][word2.length];\n};","keywords":"LeetCode 72, #72 - Edit Distance, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/edit-distance |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/edit-distance/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0072-edit-distance.js |
| String | https://leetcodejavascript.com/tags/string |
| Dynamic Programming | https://leetcodejavascript.com/tags/dynamic-programming |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow