1.5 One Away: There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.
bool oneAway(string str1, string str2){
unordered_map<char, int> m;
for(char c: str1){
m[c]++;
}
for(char c: str2){
m[c]--;
}
int count = 0;
for(auto it=m.begin();it!=m.end();it++){
count+=it->second;
}
return count==1 || count==0 || count==-1;
}
博客提及对字符串可进行插入、删除、替换字符三种编辑操作,给出一个函数需求,即检查两个字符串是否处于一次编辑(或零次编辑)距离,并提供了相关代码链接。

459

被折叠的 条评论
为什么被折叠?



