Given two strings S and T, determine if they are both one edit distance apart.

分析:https://segmentfault.com/a/1190000003906621

虽然我们可以用Edit Distance的解法,看distance是否为1,但Leetcode中会超时。这里我们可以利用只有一个不同的特点在O(N)时间内完成。如果两个字符串只有一个编辑距离,则只有两种情况:

  1. 两个字符串一样长的时候,说明有一个替换操作,我们只要看对应位置是不是只有一个字符不一样就行了

  2. 一个字符串比另一个长1,说明有个增加或删除操作,我们就找到第一个对应位置不一样的那个字符,如果较长字符串在那个字符之后的部分和较短字符串那个字符及之后的部分是一样的,则符合要求

  3. 如果两个字符串长度差距大于1,肯定不对

 public class Solution {
public boolean isOneEditDistance(String s, String t) {
int m = s.length(), n = t.length();
if(m == n) return isOneModified(s, t);
if(m - n == ) return isOneDeleted(s, t);
if(n - m == ) return isOneDeleted(t, s);
// 长度差距大于2直接返回false
return false;
} private boolean isOneModified(String s, String t){
boolean modified = false;
// 看是否只修改了一个字符
for(int i = ; i < s.length(); i++){
if(s.charAt(i) != t.charAt(i)){
if(modified) return false;
modified = true;
}
}
return modified;
} public boolean isOneDeleted(String longer, String shorter){
// 找到第一组不一样的字符,看后面是否一样
for(int i = ; i < shorter.length(); i++){
if(longer.charAt(i) != shorter.charAt(i)){
return longer.substring(i + ).equals(shorter.substring(i));
}
}
return true;
}
}

另一种方法也挺好的:http://www.programcreek.com/2014/05/leetcode-one-edit-distance-java/

 public boolean isOneEditDistance(String s, String t) {
if (s == null || t == null) return false; int m = s.length(), n = t.length();
if (Math.abs(m - n) > ) return false; int i = , j = , count = ;
while (i < m && j < n) {
if (s.charAt(i) == t.charAt(j)) {
i++;
j++;
} else {
count++;
if (count > ) {
return false;
} if (m > n) {
i++;
} else if (m < n) {
j++;
} else {
i++;
j++;
}
}
} if (i < m || j < n) {
count++;
} if (count == ) {
return true;
} return false;
}

One Edit Distance的更多相关文章

  1. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  2. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  3. Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  4. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  5. LintCode Edit Distance

    LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...

  6. stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)

    I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...

  7. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  8. 动态规划 求解 Minimum Edit Distance

    http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...

  9. 【leetcode】Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

随机推荐

  1. Spring入门_04_注解注入

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  2. 关于VS打开cshtml出现 未能完成该操作。无效指针

    关于VS打开cshtml出现 未能完成该操作.无效指针 第一步:关闭VS 第二部:删除%LocalAppData%\Microsoft\VisualStudio\14.0\ComponentModel ...

  3. cas单点登录 SSO 的实现原理

    原文出处: cutesource   欢迎分享原创到伯乐头条 单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户 ...

  4. Promise编程基础

    (一) Promise基础     所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果.从语法上说,Promise是一个对象,从它可以获取异步操作的消 ...

  5. Win8/Win10无法打开这个应用 内置管理员账户

    现在装win10系统的同伴越来越多了,相比于win7,win10在某些设置方面也有些变化,比如我们在使用win8或者win10时,会碰到如图所示的对话框: Windows10/Windows8无法使用 ...

  6. php去掉字符串的最后一个字符附substr()的用法

    转自:http://www.jb51.net/article/26604.htm 今天项目中用到,去掉字符串中的最后一个字符 原字符串1,2,3,4,5,6, 去掉最后一个字符"," ...

  7. 【转】CSS3动画帧数科学计算法

    本文来源于:财付通TID 原作者:bboy90 总结都浓缩在这个工具里了,想知道工具的地址或想窥探工具诞生的趣事请往下看 . —————————————————————–     华丽丽的开篇     ...

  8. Access应用日志<一>

    今天在确认实习生不能帮忙搭建数据库后,自己根据业务需求尝试搭了一个小型access数据库. 主要目的:储存历史月度数据,避免每次从公司数据库下载数据的麻烦,节省数据拉取时间. 搭建了以acct id为 ...

  9. jdk版本及编译版本导致服务器部署UnsupportedClassVersionError错误

    java本地代码运行正常,部署到服务器无法运行,错误如下: Caused by: java.lang.UnsupportedClassVersionError: com/teshehui/cms/ac ...

  10. 64.SHELL

    SHELL 1. crontab定时器 编辑使用crontab -e 一共6列,分别是:分 时 日 月 周 命令 查看使用crontab -l 删除任务crontab -r 查看crontab执行日志 ...