<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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> word1 = &quot;horse&quot;, word2 = &quot;ros&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> 
horse -&gt; rorse (replace &#39;h&#39; with &#39;r&#39;)
rorse -&gt; rose (remove &#39;r&#39;)
rose -&gt; ros (remove &#39;e&#39;)
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> word1 = &quot;intention&quot;, word2 = &quot;execution&quot;
<strong>Output:</strong> 5
<strong>Explanation:</strong> 
intention -&gt; inention (remove &#39;t&#39;)
inention -&gt; enention (replace &#39;i&#39; with &#39;e&#39;)
enention -&gt; exention (replace &#39;n&#39; with &#39;x&#39;)
exention -&gt; exection (replace &#39;n&#39; with &#39;c&#39;)
exection -&gt; execution (insert &#39;u&#39;)
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= word1.length, word2.length &lt;= 500</code></li>
	<li><code>word1</code> and <code>word2</code> consist of lowercase English letters.</li>
</ul>

class Solution:
    def minDistance(self, word1: str, word2: str) -> int:

        # SHORTCUT 1:
        # "delete from word1 or add to word1, to make it the same as word2"
        # is functionally equivalent to "delete from word1 or delete
        # from word2, to make them the same"

        # SHORTCUT 2:
        # "replace == delete and add" === "a delete from both strings
        # at the same time is only 1 point, not 2"
        
        # we borrow some code from #583:

        @cache
        def MD(i: int, j: int) -> int:
            nonlocal word1, word2
            try:
                A = word1[i]
            except IndexError:
                newJ = len(word2)   # skip entirety of other list
                return newJ - j
                
            try:
                B = word2[j]
            except IndexError:
                newI = len(word1)   # skip entirety of other list
                return newI - i

            if A == B:
                return 0 + MD(i + 1, j + 1)
            
            answers = []

            try:
                newI = word1.index(B, i)
                answers.append(
                    newI - i + MD(newI, j)  # skip to letter B in word 1
                )
            except ValueError:
                newI = len(word1)   # skip entirety of both remaining lists
                newJ = len(word2)
                answers.append(newI - i + newJ - j)

            try:
                newJ = word2.index(A, j)
                answers.append(
                    newJ - j + MD(i, newJ)  # skip to letter A in word 2
                )
            except ValueError:
                newI = len(word1)   # skip entirety of both remaining lists
                newJ = len(word2)
                answers.append(newI - i + newJ - j)

            answers.append(
                1 + MD(i + 1, j + 1)    # skip 1 letter in both: COUNTS AS 1 CHANGE
            )
            return min(answers)

        return MD(0, 0)

17387