[LC] 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.
class Solution {
public boolean isOneEditDistance(String s, String t) {
if (Math.abs(s.length() - t.length()) > 1) {
return false;
}
if (s.length() == t.length()) {
return isOneModify(s, t);
} else if (s.length() > t.length()) {
return isOneDel(s, t);
} else {
return isOneDel(t, s);
}
}
private boolean isOneModify(String s, String t) {
int diff = 0, i = 0;
while (i < s.length()) {
if (s.charAt(i) != t.charAt(i)) {
diff += 1;
}
i += 1;
}
return diff == 1;
}
private boolean isOneDel(String s, String t) {
int i = 0;
for (; i < s.length() && i < t.length(); i++) {
if (s.charAt(i) != t.charAt(i)) {
break;
}
}
return s.substring(i + 1).equals(t.substring(i));
}
}
[LC] 161. One 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. 给定两个字符串,判断他们是否是一步变换得到 ...
- 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
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 ...
- [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] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- 使用Python绘制新型冠状肺炎全国增长趋势图
截至1月28日24时,国家卫生健康委收到31个省(区.市)累计报告确诊病例5974例,现有重症病例1239例,累计死亡病例132例,累计治愈出院103例.现有疑似病例9239例.目前累计追踪到密切接触 ...
- java.lang.SecurityException: Permission denied (missing INTERNET permission?)
ndroid app里试图用HttpUrlConnection获取网络连接,忘记在AndroidManifest清单文件里声明需要用到Internet的权限,运行时报此错误. 解决方法 在Androi ...
- Unity3D一些基本的概念和一些基本操作
场景:整个游戏由场景组成,一个游戏至少要有一个场景,如果把所有的游戏画面放在一个场景里也是可以的,如果游戏非常非常的大,如果所有的东西都放到一个场景里那么结构就不是那么清晰了而且处理起来就会麻烦一些, ...
- linux-文件系统和目录
linux目录说明 /:系统根目录 /usr:用户的程序 /home:默认创建用户在此目录下创建用户主目录 /etc:存放系统配置文件.服务脚本,一些程序配置文件 /bin:常用命令 /sbin:常用 ...
- one_day_one_linuxCmd---光标快捷操作
<坚持每天学习一个 linux 命令,今天我们来学习 切换光标的常用命令> 摘要:最近经常使用 xshell 软件来远程连接各种机器,在 bin/bash 下输入各种命令,因为都是一些非常 ...
- Centos配置NAT模式下的静态ip
一.查看所在的ip段 点击 编辑-->虚拟网卡编辑器 选中vmware8网卡,点击 DHCP设置 二.编辑网卡配置文件 查看网卡 ip addr 命令打开配置文件 vi /etc/sysconf ...
- Redis_大保健
Redis redis命令参考网址: http://doc.redisfans.com/ redis主从: 集群:一组通过网络连接的计算机,共同对外提供服务,像一个独立的服务器. 一.简介 nosql ...
- MVPR下的PHP分页教程
这个PHP分页其实不难,现在就开始看看核心思路吧. 我习惯从最底层开始看起. 1. 首先用LIMIT偏移QUERY的指针 /* * get hot post by current page * @pa ...
- JS 日期格式化为 2020-11-01 22:33:44 格式
项目中经常会用到将JS日期格式化输出为 标准格式(2020-11-01 22:33:44)的问题.写个精简版的,代码如下: function formatDate(d){ d = d || new D ...
- 第一个struts2框架
编写步骤: 1.导入有关的包. 2.编写web.xml文件 3.写Action类 4.编写jsp 5.编写struts.xml web.xml <?xml version="1.0&q ...