题目:
字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 给定两个字符串,编写一个函数判定它们是否只需要一次(或者零次)编辑。
示例 1:
输入:
first = "pale"
second = "ple"
输出: True
示例 2:
输入:
first = "pales"
second = "pal"
输出: False
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/one-away-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Python代码:
class Solution(object):
def oneEditAway(self, first, second):
"""
:type first: str
:type second: str
:rtype: bool
"""
m, n = len(first), len(second)
if(abs(m-n)>=2):
return False
for index in range(min(m, n)):
if first[index] != second[index]:
return first[index+1:]==second[index+1:] or first[index:]==second[index+1:] or first[index+1:]==second[index:]
return True
网友评论