<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>

import heapq


class PQ:
   def __init__(self, items, key=lambda x:x):
       self._key = key
       self._data = []
       self._seq = -1
       for item in items:
           self._seq += 1
           self._data.append((self._key(item), self._seq, item))
       heapq.heapify(self._data)
  
   def pop(self):
       return heapq.heappop(self._data)[2]
  
   def push(self, item):
       self._seq += 1
       heapq.heappush(self._data, (self._key(item), self._seq, item))
  
   def peek(self):
       return self._data[0][2]


   def replace(self, item):
       self._seq += 1
       return heapq.heapreplace(self._data, (self._key(item), self._seq, item))


   def empty(self):
       return len(self._data) == 0
  
   def __repr__(self) -> str:
       return self.__str__()


   def __str__(self) -> str:
       return f"[{','.join([str(x[2]) for x in self._data])}]"


   def __len__(self):
       return len(self._data)

class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        queue = PQ([], key=lambda x:x[2]) #min PQ on cost
        processed = set()
        queue.push((0,0,0))# word1_length, word2_length, mutations
        while queue:
            w1, w2, mutations = queue.pop()
            processed.add((w1,w2))
            if w1 == len(word1) and w2 == len(word2):
                return mutations

            next_states = [(w1+1, w2, mutations+1), # deletion
                            (w1,w2+1, mutations+1)  # insertion
                            ]
            if w1 < len(word1) and w2 < len(word2):
                replace_cost = 0 if word1[w1] == word2[w2] else 1
                next_states.append((w1+1, w2+1, mutations+replace_cost))

            for v1, v2, cost in next_states:
                if v1 > len(word1) or v2 > len(word2) or (v1,v2) in processed:
                    continue

                queue.push((v1,v2,cost))
        
        raise Exception("Should never come here")

23012