"""字符串有三種編輯操作:插入一個字符、刪除一個字符或者替換一個字符。 給定兩個字符串,編寫一個函數判定它們是否只需要一次(或者零次)編輯。
示例 1:
輸入:
first = "pale"
second = "ple"
輸出: True
示例 2:
輸入:
first = "pales"
second = "pal"
輸出: False
"""
class Solution:
def get_flag(self,first,second):#獲取兩個字典中不同的個數 eg:建值一個存在一個不存在 value不同
count = 0
if len(first)>len(second):
if second in first:
return True
elif len(first)<len(second):
if first in second:
return True
while len(first)>1 or len(second)>1:
if first[0]==second[0]:
first = first[1:]
second = second[1:]
else:
count+=1
first = first[::-1]
second = second[::-1]
print("first:",first)
print("second:",second)
if count == 2 :
return False
return True
def oneEditAway(self, first: str, second: str) -> bool:
if abs(len(first)-len(second))>1:
flag = False
else :
flag = self.get_flag(first,second)
return flag
A = Solution()
print(A.oneEditAway("ab","bc"))
leetcode 面試一次編輯01.05python3
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.