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. 初识 Angular 体会

    一句话描述:一个前端的类似MVC框架的JS库 刚接触2天,刚一看感觉和asp.net mvc能实现的功能有点重复. 虽然asp.net的表单验证,Razor语法使其在前端开发有较大提升,但要实现比较高 ...

  2. [原创] SQLite数据库使用清单(下)

    上文两章对SQLite的功能.语法.和操作进行了介绍,本文讲解SQLite的一些高级语法和操作. 3.

  3. mksquash_lzma-3.2 编译调试记录

    今天在编译mksquash_lzma-3.2的时候出现了如下问题: /home/test/RT288x_SDK/toolchain/mksquash_lzma-3.2/lzma443/C/7zip/C ...

  4. AFN的坑--NSCachedURLResponse缓存

    网络正常的情况下,如果服务器宕机或者数据库出错,会造成访问服务器报错的情况,一般报错的内容是:无法连接到服务器或者其它错误.且服务器 修复后,仍然报错.经过排查,终于找出了原因所在:AFNetwork ...

  5. SGU 155.Cartesian Tree

    时间限制:0.25s 空间限制:6M 题意: 给出n(n< 50000)个含双关键字(key,val)的节点,构造一颗树使该树,按key值是一颗二分查找树,按val值是一个小根堆. Soluti ...

  6. 泛型? extents super

    ?可以接受任何泛型集合,但是不能编辑集合值.所以一般只在方法参数中用 例子: ? extends Number  则类型只能是Number类的子孙类 ? super String  则类型只能是Str ...

  7. this详解:JAVASCRIPT中的this到底是谁?

    语法 this 全局对象 在全局执行上下文(函数之外),this引用的是全局对象. console.log(this.document === document); // true // In web ...

  8. iOS 多线程详解

    iOS开发 多线程 概览 机器码是按顺序执行的,一个复杂的多步操作只能一步步按顺序逐个执行.改变这种状况可以从两个角度出发: 对于单核处理器,可以将多个步骤放到不同的线程,这样一来用户完成UI操作后其 ...

  9. BZOJ 1070 修车

    Description 同一时刻有\(N\)位车主带着他们的爱车来到了汽车维修中心.维修中心共有\(M\)位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这\(M\)位技术 ...

  10. google yeoman

    Yeoman是Google的团队和外部贡献者团队合作开发的,他的目标是通过Grunt(一个用于开发任务自动化的命令行工具)和Bower(一个HTML.CSS.Javascript和图片等前端资源的包管 ...