<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 {
public:
    //recursion, top-down
    //time complexity: O(m*n)
    
    int helper(int indx1, int indx2,  string word1, string& word2, vector<vector<int>>& v)
    {
        if (indx2<0 && indx1>=0)
            return indx1+1;

        if (indx2<0 && indx1<0)
            return 0;
        
        if (indx1<0 && indx2>=0)
            return indx2+1;
        
        //if (word1 == word2)
            //return 0;

        if (v[indx1][indx2] != -1)
            return v[indx1][indx2];

        if (word1[indx1] == word2[indx2])
            return v[indx1][indx2] = helper(indx1-1, indx2-1, word1, word2, v);
        else
        {
            //insert
            //int i = helper(indx1, indx2-1, word1.insert(indx1+1, 1, word2[indx2]), word2, v);
            int i = helper(indx1, indx2-1, word1, word2, v);
            //delete
            //int d = helper(indx1-1, indx2, word1.erase(indx1,1), word2, v);
            int d = helper(indx1-1, indx2, word1, word2, v);
            //replace
            //int r = helper(indx1-1, indx2-1, word1.replace(indx1, 1, word2,indx2, 1), word2, v);
            int r = helper(indx1-1, indx2-1, word1, word2, v);
            return v[indx1][indx2] = 1 + min(r, min(i, d));
        }
    }

    int minDistance(string word1, string word2) 
    {
        if (word1.length() == 0)
            return word2.length();

        if (word2.length() == 0)
            return word1.length();
        
        int len1 = word1.length();
        int len2 = word2.length();

        vector<vector<int>> v(len1, vector<int>(len2, -1));

        return helper(len1-1, len2-1, word1, word2, v);
    }
    

    //iteration, bottom-up
    //time complexity: O(m*n)
    /*
    int minDistance(string word1, string word2) 
    {
        if (word1.length() == 0)
            return word2.length();

        if (word2.length() == 0)
            return word1.length();
        
        int m = word1.length();
        int n = word2.length();

        vector<vector<int>> v(m+1, vector<int>(n+1));
        
        for (int i=0;i<=m;++i)
            v[i][0]=i;
        
        for (int j=0;j<=n;++j)
            v[0][j]=j;

        for (int i=1;i<=m;++i)
        {
            for (int j=1;j<=n;++j)
            {
                if (word1[i-1] == word2[j-1])
                    v[i][j] = v[i-1][j-1];
                else
                    v[i][j] = 1+ min(v[i-1][j-1], min(v[i-1][j], v[i][j-1]));
            }
        }

        return v[m][n];
    }
    */
};
47491