One Edit Distance

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

分析:

  编辑距离复杂度为O(MN),而本题显然不能用这么高的复杂度;首先,可以通过判断两个字符串是否等长来决定用增一位、减一位、替换一位这三种方法之一来使得两个字符串等同,如果都不行,就return false;然后同时遍历S和T,第一次遇到不匹配的,就用刚才判断出的方法拯救一下;第二次还遇到不匹配的,就直接return false;如果到最后都没一次不匹配的,也return false;如果到最后有一次不匹配,return true。时间复杂度为O(n),额外空间复杂度为O(1)。

代码:

bool oneEditDistance(string S, string T) {
int diff = int(S.length() - T.length());
if(abs(diff) > )
return false;
int i = , j = ;
bool change = false;
while(i < S.length() && j < T.length()) {
if(S[i] != T[j]) {
//因为后面总会i++, j++,所以在这里先i--就表示后面只j++,先j--就表示后面只i++
if(diff < )
i--;
else if(diff > )
j--;
//第一次修改,则修改状态改为true;第二次修改,则直接return false
if(!change)
change = true;
else
return false;
}
i++;
j++;
}
//如果没修改过,如果此时S和T都遍历完了,说明S == T,不符,return false;如果其中一个没遍历完,说明还是差1,return true;
if(!change) {
if(i < S.length() || j < T.length())
return true;
else
return false;
}
return true;
}

[Locked] 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. One Edit Distance

    Given two strings S and T, determine if they are both one edit distance apart. 分析:https://segmentfau ...

随机推荐

  1. android 蓝牙4.0 开发介绍

    最近一直在研究一个蓝牙功能 由于本人是菜鸟  学起来比较忙 一直搞了好久才弄懂 , 网上对蓝牙4.0也就是几个个dome 抄来抄去,全是英文注解 , 对英语不好的朋友来说 真是硬伤 , 一些没必要的描 ...

  2. Gprinter Android SDK V2.0 使用说明

    佳博特约经销商,此店购买的打印机问题优先解决哟 https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.V1p ...

  3. eclipse中更改默认编码格式

    更改过程如下: (1)window->preferences->general->content Types, 选中java class file修改default encoding ...

  4. Linux系统swap已分区但无法挂载与cryptswap1问题

    linux下察看swap分区大小的命令 top 或者fdisk -l 或者free -m SWAP分区一般大小为物理内存的2倍,但最大不超过2G; 增加SWAP空间的方法有两个:增加另外一个SWAP分 ...

  5. js获取标签的几种方式

    一:id获取(全部浏览器兼容) document.getElementById(""); <body> <div id="box">&l ...

  6. 搞一个app需要多久?

    //转载文章,看后有感 我有些尴尬地拿着水杯,正对面坐着来访的王总,他是在别处打拼的人,这几年据说收获颇丰,见移动互联网如火如荼,自然也想着要进来干一场,尽管王总从事的行当也算跟IT沾边,但毕竟太长时 ...

  7. iOS 里面如何使用第三方应用程序打开自己的文件,调用wps其他应用打开当前应用里面的的ppt doc xls

    我们的自己的应用里面经常涉及的要打开ppt doc,这样的功能,以前总以为iOS沙盒封闭化,不可能实现,后来终于解决了 使用 UIDocumentInteractionController 来解决这一 ...

  8. JSP标准标签库的安装以及自定义标签的创建

    JSTL 库安装 Apache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip). 官方下载 ...

  9. linux指令备份

    ls -a 显示隐藏文件 cd 回到当前用户的目录 /home/ubuntu touch 创建文件 cat Hello.javamore/less Hello.java分页显示 grep root / ...

  10. location传值

    location.href="url?p="+"value"; onclick="location.href='Card_query_where?qu ...