"""字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 给定两个字符串,编写一个函数判定它们是否只需要一次(或者零次)编辑。
示例 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
最新推荐文章于 2022-06-10 11:54:40 发布