<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of <strong>steps</strong> required to make</em> <code>word1</code> <em>and</em> <code>word2</code> <em>the same</em>.</p>

<p>In one <strong>step</strong>, you can delete exactly one character in either string.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> word1 = &quot;sea&quot;, word2 = &quot;eat&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> You need one step to make &quot;sea&quot; to &quot;ea&quot; and another step to make &quot;eat&quot; to &quot;ea&quot;.
</pre>

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

<pre>
<strong>Input:</strong> word1 = &quot;leetcode&quot;, word2 = &quot;etco&quot;
<strong>Output:</strong> 4
</pre>

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

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

class Solution {
public:

    string s1, s2;
    int n1, n2;
    unordered_map<string, int> memo;

    string getKey(int i, int j) {
        return to_string(i) + " " + to_string(j);
    }

    int minDistance(string word1, string word2) {
        s1 = word1, s2 = word2;
        n1 = s1.length(), n2 = s2.length();
        return minDel(0, 0);
    }

    int minDel(int i1, int i2) {
        if (i1 == n1) return n2 - i2;
        if (i2 == n2) return n1 - i1;

        string key = getKey(i1, i2);
        if (memo.find(key) != memo.end()) return memo[key];

        if (s1[i1] == s2[i2]) memo[key] = minDel(i1 + 1, i2 + 1);
        else memo[key] = 1 + min(minDel(i1 + 1, i2), minDel(i1, i2 + 1));

        return memo[key];
    }
};
80951