Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Created February 5, 2023 17:37
Show Gist options
  • Save mvallebr/93869ffef420789d9dcb7cd32b016b4e to your computer and use it in GitHub Desktop.
Save mvallebr/93869ffef420789d9dcb7cd32b016b4e to your computer and use it in GitHub Desktop.
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
memo = {}
def dfs(offset1, offset2):
if (offset1, offset2) in memo:
return memo[(offset1, offset2)]
if len(word1) == offset1 or len(word2) == offset2:
return abs((len(word1)-offset1)-(len(word2)-offset2))
if word1[offset1] == word2[offset2]:
result = dfs(offset1 + 1, offset2 + 1,)
memo[(offset1+1, offset2+1)] = result
return result
num_steps1 = 1 + dfs(offset1 + 1, offset2)
num_steps2 = 1 + dfs(offset1, offset2 + 1)
result = min(num_steps1, num_steps2)
memo[(offset1, offset2)] = result
return result
return dfs(0, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment