[LC] 161. One Edit Distance
2024-10-08 20:49:43
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));
}
}
最新文章
- slidedoor滑动门特效
- NYOJ 205
- 对CPU做下性能测试
- JBox - 模态窗口,工具提示和消息 jQuery 插件
- Retrofit与RXJava整合
- Winform开发框架的重要特性总结
- ASP.Net 验证视图状态 MAC 失败
- 实现长按删除QListWidget的Item
- [git] fatal: This operation must be run in a work tree
- [置顶] android调用第三方库——第四篇——调用多个第三方库
- MFC控件实现视频“暂停” “播放”循环
- openvpn服务器一键脚本生成客户端文件
- unix scp命令(两个unix系统传输文件)
- SqlSever查询某个表的列名称、说明、备注、注释,类型等
- Android P 功能和 API
- python2 python3 字符串 编码格式 处理
- Win7没有防火墙:0x80070422
- javascript简要笔记
- jenkins Process leaked file descriptors
- linux的一些操作
热门文章
- hook键盘钩子_非dll
- sql注入入门--基本命令
- 吴裕雄--天生自然 JAVASCRIPT开发学习: 验证 API
- UML-GRASP后4种模式
- [De1CTF 2019]SSRF Me-MD5长度扩展攻击&;CVE-2019-9948
- Linux(CENTOS7) Redis安装
- 从定时器的选型,到透过源码看XXL-Job(上)
- 计蒜客 密码锁(BFS)
- subprocess.Popen stdout重定向内容实时获取
- 12 Spring Data JPA:orm思想和hibernate以及jpa的概述和jpa的基本操作