Suppose we have two strings S and T we have to check whether they are one or zero edit distance away or not. An edit operation can be defined as deleting a character, adding a character, or replacing a character with another character.
So, if the input is like S = "hello", T = "hallo", then the output will be True, as these two strings have edit distance of 1.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, S, T): m, n = len(S), len(T) i, j = 0, 0 count = 0 if abs(m - n) > 1: return False while i < m and j < n: if S[i] != T[j]: if count == 1: return False if m < n: j += 1 elif m > n: i += 1 else: i += 1 j += 1 count += 1 else: i += 1 j += 1 return True ob = Solution() S = "hello" T = "hallo" print(ob.solve(S, T))