Edit Distance LeetCode solution in Python
1from functools import cache234class Solution:5def minDistance(self, word1: str, word2: str) -> int:6m, n = len(word1), len(word2)78@cache9def dp(i, j):10if i == 0:11return j12if j == 0:13return i1415l1, l2 = word1[i - 1], word2[j - 1]16if l1 != l2:17return 1 + min(18dp(i, j - 1), # Delete / insert j19dp(i - 1, j), # Delete / insert i20dp(i - 1, j - 1), # Replace i or j21)22return dp(i - 1, j - 1)2324return dp(m, n)