161-One-Edit-Distance

Sat 17 May 2025

https://leetcode.com/problems/one-edit-distance

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isOneEditDistance(self, s: str, t: str) -> bool:
    m = len(s)
    n = len(t)
    if m > n:  # Make sure len(s) <= len(t)
      return self.isOneEditDistance(t, s)

    for i in range(m):
      if s[i] != t[i]:
        if m == n:
          return s[i + 1:] == t[i + 1:]  # Replace s[i] with t[i]
        return s[i:] == t[i + 1:]  # Delete t[i]

    return m + 1 == n  # Delete t[-1]
new Solution().isOneEditDistance()

Score: 5

Category: leetcode