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:

  1. Insert a character into s to get t
  2. Delete a character from s to get t
  3. 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));
}
}

最新文章

  1. slidedoor滑动门特效
  2. NYOJ 205
  3. 对CPU做下性能测试
  4. JBox - 模态窗口,工具提示和消息 jQuery 插件
  5. Retrofit与RXJava整合
  6. Winform开发框架的重要特性总结
  7. ASP.Net 验证视图状态 MAC 失败
  8. 实现长按删除QListWidget的Item
  9. [git] fatal: This operation must be run in a work tree
  10. [置顶] android调用第三方库——第四篇——调用多个第三方库
  11. MFC控件实现视频“暂停” “播放”循环
  12. openvpn服务器一键脚本生成客户端文件
  13. unix scp命令(两个unix系统传输文件)
  14. SqlSever查询某个表的列名称、说明、备注、注释,类型等
  15. Android P 功能和 API
  16. python2 python3 字符串 编码格式 处理
  17. Win7没有防火墙:0x80070422
  18. javascript简要笔记
  19. jenkins Process leaked file descriptors
  20. linux的一些操作

热门文章

  1. hook键盘钩子_非dll
  2. sql注入入门--基本命令
  3. 吴裕雄--天生自然 JAVASCRIPT开发学习: 验证 API
  4. UML-GRASP后4种模式
  5. [De1CTF 2019]SSRF Me-MD5长度扩展攻击&amp;CVE-2019-9948
  6. Linux(CENTOS7) Redis安装
  7. 从定时器的选型,到透过源码看XXL-Job(上)
  8. 计蒜客 密码锁(BFS)
  9. subprocess.Popen stdout重定向内容实时获取
  10. 12 Spring Data JPA:orm思想和hibernate以及jpa的概述和jpa的基本操作