[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 ...
随机推荐
- Android 5.0以上手机出现找不到so文件
问题描述 最近做项目出了一个bug项目中用到so文件,在5.0以上的手机上会报一个初始化异常错误,并提示找不到so文件.lib里目录结构类似如下 在Android5以下都没有问题,在5.0以上会报错 ...
- C# - Sql数据类型的对应关系
<Language From="SQL" To="C#"> <Type From="bigint" To="lo ...
- maven项目下tomcat直接启动不了(LifecycleException)。报错如下截图
经查,tomcat项目下的lib中没有jar包,发布的时候没有将jar包发布上去.这个问题在我的博客中以前遇到过.如何将maven的jar发布到项目中,我的博客里面有记载
- iOS 百度地图监听地图状态
百度地图提供了地图状态的对象BMKMapStatus ///此类表示地图状态信息 @interface BMKMapStatus : NSObject { float _fLevel; // 缩放比例 ...
- 【转】iOS8 推送 获取 devicetoken
标签:推送 push ios8 devicetoken token xcode6 原文:http://roybaby.blog.51cto.com/1508945/1557854 打开AppDeleg ...
- linux常用指令(飞天云)
1.svn相关指令 svn co svn://... //check out 到本地 2.pangu相关 pu cpdir pangu://... //复制pangu里面对应的文件夹到本 ...
- ICE学习第四步-----客户端请求服务器返回数据
这次我们来做一个例子,流程很简单:客户端向服务器发送一条指令,服务端接收到这条指令之后,向客户端发送数据库中查询到的数据,最终显示在DataGridView上. 根据上一篇文章介绍的Slice语法,我 ...
- jquery中的ajax方法详解
定义和用法ajax() 方法通过 HTTP 请求加载远程数据.该方法是 jQuery 底层 AJAX 实现.简单易用的高层实现见 $.get, $.post 等.$.ajax() 返回其创建的 XML ...
- Django Web在Apache上的部署
1. 安装配置Apache 2. 安装wsgi_mod模块 3. 开放相应端口 vim /etc/sysconfig/iptables # Firewall configuration written ...
- 151111 sqlite3数据库学习
最近在学习数据库,想起去年做的项目里用到了sqlite3.那时候,没有任何的数据库经验,误打误撞,找到了sqlite3,然后参考网络上零碎的信息,把它嵌入到工程里,并且成功了.可惜,那时候没有好好保存 ...