Skip to content

Instantly share code, notes, and snippets.

@jsaluja
Created July 17, 2024 19:55
Show Gist options
  • Save jsaluja/5ad1f3b81e4e650f12ae38876da935bd to your computer and use it in GitHub Desktop.
Save jsaluja/5ad1f3b81e4e650f12ae38876da935bd to your computer and use it in GitHub Desktop.
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
def dfs(i: int, j: int) -> int:
if i == len(word1):
return len(word2) - j
if j == len(word2):
return len(word1) - i
if memo[i][j] is not None:
return memo[i][j]
ans = 0
if word1[i] == word2[j]:
ans = dfs(i + 1, j + 1)
else:
ans = 1 + min(dfs(i + 1, j + 1), min(dfs(i + 1, j), dfs(i , j + 1)))
memo[i][j] = ans
return ans
memo = [[None] * len(word2) for _ in range(len(word1))]
return dfs(0, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment