题目描述
字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 给定两个字符串,编写一个函数判定它们是否只需要一次(或者零次)编辑。
示例 1:
输入:
first = "pale"
second = "ple"
输出: True
示例 2:
输入:
first = "pales"
second = "pal"
输出: False
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/one-away-lcci
方法一:
class Solution {
public:
bool oneEditAway(string first, string second) {
int count=0,l=first.length()-second.length();
if(l>1||l<-1){
return false;
}
for(int i=0,j=0;i<first.length()&&j<second.length();i++,j++){
if(first[i]!=second[j]){
if(l>0){
j--;
}
if(l<0){
i--;
}
count++;
}
}
if(count<=1) return true;
else return false;
}
};