程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Leetcode question solution (interview 01.05): judge whether a string can be transformed into a specified string through one edit (Python)

編輯:Python


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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.




  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved