[LeetCode] 161. One Edit Distance 一个编辑距离
Given two strings s and t, determine if they are both one edit distance apart.
Note:
There are 3 possiblities to satisify one edit distance apart:
- Insert a character into s to get t
- Delete a character from s to get t
- Replace a character of s to get t
Example 1:
Input: s = "ab", t = "acb"
Output: true
Explanation: We can insert 'c' into s to get t.
Example 2:
Input: s = "cab", t = "ad"
Output: false
Explanation: We cannot get t from s by only one step.
Example 3:
Input: s = "1203", t = "1213"
Output: true
Explanation: We can replace '0' with '1' to get t.
这道题是之前那道 Edit Distance 的拓展,然而这道题并没有那道题难,这道题只让我们判断两个字符串的编辑距离是否为1,那么只需分下列三种情况来考虑就行了:
1. 两个字符串的长度之差大于1,直接返回False。
2. 两个字符串的长度之差等于1,长的那个字符串去掉一个字符,剩下的应该和短的字符串相同。
3. 两个字符串的长度之差等于0,两个字符串对应位置的字符只能有一处不同。
分析清楚了所有的情况,代码就很好写了,参见如下:
解法一:
class Solution {
public:
bool isOneEditDistance(string s, string t) {
if (s.size() < t.size()) swap(s, t);
int m = s.size(), n = t.size(), diff = m - n;
if (diff >= ) return false;
else if (diff == ) {
for (int i = ; i < n; ++i) {
if (s[i] != t[i]) {
return s.substr(i + ) == t.substr(i);
}
}
return true;
} else {
int cnt = ;
for (int i = ; i < m; ++i) {
if (s[i] != t[i]) ++cnt;
}
return cnt == ;
}
}
};
我们实际上可以让代码写的更加简洁,只需要对比两个字符串对应位置上的字符,如果遇到不同的时候,这时看两个字符串的长度关系,如果相等,则比较当前位置后的字串是否相同,如果s的长度大,那么比较s的下一个位置开始的子串,和t的当前位置开始的子串是否相同,反之如果t的长度大,则比较t的下一个位置开始的子串,和s的当前位置开始的子串是否相同。如果循环结束,都没有找到不同的字符,那么此时看两个字符串的长度是否相差1,参见代码如下:
解法二:
class Solution {
public:
bool isOneEditDistance(string s, string t) {
for (int i = ; i < min(s.size(), t.size()); ++i) {
if (s[i] != t[i]) {
if (s.size() == t.size()) return s.substr(i + ) == t.substr(i + );
if (s.size() < t.size()) return s.substr(i) == t.substr(i + );
else return s.substr(i + ) == t.substr(i);
}
}
return abs((int)s.size() - (int)t.size()) == ;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/161
类似题目:
参考资料:
https://leetcode.com/problems/one-edit-distance/
https://leetcode.com/problems/one-edit-distance/discuss/50108/C%2B%2B-DP
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 161. One Edit Distance 一个编辑距离的更多相关文章
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [leetcode]161. One Edit Distance编辑步数为一
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
- [LeetCode#161] One Edit Distance
Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...
- 【LeetCode】161. One Edit Distance
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...
- 161. One Edit Distance
题目: Given two strings S and T, determine if they are both one edit distance apart. 链接: http://leetco ...
- [LeetCode] 161. One Edit Distance_Medium
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- [LC] 161. One Edit Distance
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
随机推荐
- 如何配置jdk的本地环境
在计算机→属性→高级系统设置→高级→环境变量,如图: 第一步:系统变量→新建 JAVA_HOME 变量 . 变量值填写jdk的安装目录(本人是C:\Program Files\Java\jdk1.8. ...
- tensorboard--打开训练的日志文件
tensorboard --logdir=logs 注意:等号之间不要空格.
- IIS锁定是默认设置的 (overrideModeDefault="Deny")问题解决
发布网站时提示错误 锁定是默认设置的 (overrideModeDefault="Deny"),或者是通过包含 overrideMode="Deny" 或旧有的 ...
- Zabbix 设置自动添加主机两种方法(自动注册、自动发现)
在实际生产环境中,我们可能需要将很多台主机添加到 Zabbix Server 里,我们进行手动添加的话,会比较麻烦.费时,而且还容易出错.所以一般我们会设置主机自动注册.这样就比较方便. 官方文档链接 ...
- Winform中设置ZedGraph当前所有曲线的颜色
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- centos下非yum方式安装docker环境
1.下载docker源码包 docker官网地址: https://download.docker.com/linux/static/stable/ 选择.tgz的包下载,例如:https://dow ...
- rhel安装输入法
# yum install "@Chinese Support" 安装完成后,设置输入法: System -> Preferences -> Input Method
- 图解Java数据结构之稀疏数组
在编程中,算法的重要性不言而喻,没有算法的程序是没有灵魂的.可见算法的重要性. 然而,在学习算法之前我们需要掌握数据结构,数据结构是算法的基础. 我在大学的时候,学校里的数据结构是用C语言教的,因为对 ...
- 章节十四、2-自动完成功能-Autocomplete
一.什么是自动匹配功能? 很多网站都有自动匹配功能,列如你在使用天猫搜索商品时,输入“鞋”,输入框的下面会出现很多与“鞋”有关的选项. 二.以https://www.expedia.com/网站的城市 ...
- 155--MinStack
/* 解法一:使用链表从0实现栈,用min来存放最小值. 复杂的地方是,如果pop了最小的数,就要遍重新找到最小的数. */ public class MinStack { List<Integ ...