<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 minDistance(string str1, string str2) {
        int m = str1.size();
        int n = str2.size();

        map<pair<int, int>, int> dist;
        deque<tuple<int, int>> que;
        que.push_back({0, 0});
        dist[{0, 0}] = 0; // Initialize distance for the starting point

        auto neighbors = [&] (int i, int j) -> vector<tuple<int, int, int>> {
            if (i == m) {
                return {{1, m, j + 1}};
            }

            if (j == n) {
                return {{1, i + 1, n}};
            }

            if (str1[i] == str2[j]) {
                return {{0, i+1, j+1}};
            }
            
            return {{1, i+1, j}, {1, i, j+1}};
        };

        while (!que.empty()) {
            auto [r, c] = que.front();
            que.pop_front();

            if (r == m && c == n) {
                return dist[{r, c}];
            }

            for (auto [weight, nr, nc] : neighbors(r, c)) {
                int ncost = dist[{r, c}] + weight;

                if (dist.find({nr, nc}) == dist.end()) {
                    dist[{nr, nc}] = ncost;
                    (weight == 0) 
                        ? que.push_front({nr, nc}) 
                        : que.push_back({nr, nc});    
                }
            }
        }
        return -1;
    }
};
44331