[Locked] One Edit Distance
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的更多相关文章
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 编辑距离——Edit Distance
编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...
- LintCode Edit Distance
LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...
- stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)
I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- 动态规划 求解 Minimum Edit Distance
http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...
- One Edit Distance
Given two strings S and T, determine if they are both one edit distance apart. 分析:https://segmentfau ...
随机推荐
- WPF Paragraph获取或修改文本内容
一.说明 Paragraph继承自Block,Block继承自TextElement,在TextElement中 // // 摘要: // 获取表示元素中内容末尾的 System.Windows.Do ...
- (转)修改ECSHOP前后台的title中的ecshop
前台部分: 1:去掉头部TITLE部分的ECSHOP演示站 Powered by ecshop 前者在后台商店设置 - 商店标题修改 后者打开includes/lib_main.php $page_t ...
- 设置ORACLE环境变量
sqlplus 执行不了可能原因是未设置环境变量,设置方法: export ORACLE_HOME=/usr/local/instantclient_11_2
- ArcMap - 分割.
一,分割面: 1,在屏幕上新增线分割面: 使待编辑的面处于编辑状态 -> 选择待分割的面(使其处于选中状态) -> 选择编辑工具的 (Cut Polygons Tools) ->画线 ...
- 嵌入式css样式,写在当前的文件中
现在有一任务,把下面的“超酷的互联网”.“服务及时贴心”.“有趣易学”这三个短词文字字号修改为18px. 如果用内联式css样式的方法进行设置将是一件很头疼的事情(为每一个<span>标签 ...
- Codeforces 543B Destroying Roads(最短路)
题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...
- Unable to load configuration. - bean - jar: ....struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
摘录的异常代码: 2013-12-14 22:42:07 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error严重: Dis ...
- android:persistent属性
application PhoneApp既没有被Broadcast唤醒,也没有被其他service调用,那么是android是通过什么方式来启动PhoneApp,所以就发现了属性android:per ...
- Jquery 简单的Tab选项卡特效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C# 窗体间传值方法大汇总(转)
第一种方法:创建一个类,里面声明用于存储接收的字段.传的时候存储于字段中,要用的时候,直接类名.字段名 进行调用.(这种方法传递是双向的) 第二种方法:1.在Form1里定义 public strin ...