Edit Distance
You get two words. In one edit you may insert a letter, delete a letter or replace one letter with another. Find the fewest edits that turn the first word into the second. Our example: "pans" → "span" takes 2: insert s in front, delete s at the end.
- input
word1,word2: two words.- returns
- The fewest operations that turn
word1intoword2, where an operation inserts, deletes or replaces one letter. - constraints
- 0 ≤ word1.length, word2.length ≤ 100
- word1, word2: lowercase English letters
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {string} word1
* @param {string} word2
* @return {number}
*/
function minDistance(word1, word2) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more dynamic programming (dp) problems
- 70Climbing StairsEasysolve
- 121Best Time to Buy and Sell StockEasysolve
- 198House RobberMediumsolve
- 322Coin ChangeMediumsolve
- 300Longest Increasing SubsequenceMediumsolve
- 62Unique PathsMediumsolve
- 1143Longest Common SubsequenceMediumsolve
- 53Maximum SubarrayMediumsolve
- 918Maximum Sum Circular SubarrayMediumsolve
- 139Word BreakMediumsolve
- 120TriangleMediumsolve
- 64Minimum Path SumMediumsolve
- 63Unique Paths IIMediumsolve
- 97Interleaving StringMediumsolve
- 221Maximal SquareMediumsolve
- 123Best Time to Buy and Sell Stock IIIHardsolve
- 188Best Time to Buy and Sell Stock IVHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.