<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:
    int solve(int m , int n , string s1, string s2, vector<vector<int>> &dp) {
        if(m == 0 || n == 0) {
            return 0;
        }

        if(dp[m][n] != -1) {
            return dp[m][n];
        }

        if(s1[m-1] == s2[n-1]) {  // Use m-1 and n-1 to access characters
            dp[m][n] = 1 + solve(m-1, n-1, s1, s2, dp);
        } else {
            int a = solve(m, n-1, s1, s2, dp);
            int b = solve(m-1, n, s1, s2, dp);

            dp[m][n] = max(a, b);
        }

        return dp[m][n];
    }

    int longestCommonSubsequence(string text1, string text2) {
        int m = text1.size();
        int n = text2.size();

        vector<vector<int>> dp(m+1, vector<int>(n+1, -1));

        return solve(m, n, text1, text2, dp);  // Pass m, n directly
    }

    int minDistance(string word1, string word2) {
        // len1 + len2 - 2*(lcs_len);
        return word1.size()+word2.size()-2*longestCommonSubsequence(word1, word2);   
    }
};
195388