subject : The original title is link ( secondary )
label : character string 、 Dynamic programming
solution
Time complexity
Spatial complexity
Execution time
Ans 1 (Python)
O ( N )
O ( 1 )
40ms (91.41%)
Ans 2 (Python)
Ans 3 (Python)
Solution 1 :
class Solution:
def oneEditAway(self, first: str, second: str) -> bool:
N1, N2 = len(first), len(second)
if N1 == N2:
diff = False
for i in range(N1):
if first[i] != second[i]:
if diff:
return False
else:
diff = True
return True
elif N1 == N2 - 1:
diff = 0
i = 0
while i < N1:
if first[i] != second[i + diff]:
if diff:
return False
else:
diff += 1
else:
i += 1
return True
elif N2 == N1 - 1:
diff = 0
i = 0
while i < N2:
if first[i + diff] != second[i]:
if diff:
return False
else:
diff += 1
else:
i += 1
return True
else:
return False