Class Solution

java.lang.Object
g0001_0100.s0072_edit_distance.Solution

public class Solution extends Object
72 - Edit Distance.<p>Hard</p> <p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Replace a character</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;horse&rdquo;, word2 = &ldquo;ros&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> horse -> rorse (replace &lsquo;h&rsquo; with &lsquo;r&rsquo;) rorse -> rose (remove &lsquo;r&rsquo;) rose -> ros (remove &lsquo;e&rsquo;)</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;intention&rdquo;, word2 = &ldquo;execution&rdquo;</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> intention -> inention (remove &lsquo;t&rsquo;) inention -> enention (replace &lsquo;i&rsquo; with &lsquo;e&rsquo;) enention -> exention (replace &lsquo;n&rsquo; with &lsquo;x&rsquo;) exention -> exection (replace &lsquo;n&rsquo; with &lsquo;c&rsquo;) exection -> execution (insert &lsquo;u&rsquo;)</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 <= word1.length, word2.length <= 500</code></li> <li><code>word1</code> and <code>word2</code> consist of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minDistance

      public int minDistance(String w1, String w2)