[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 ...
随机推荐
- C#中的Dictionary字典类介绍
Dictionary字典类介绍 必须包含名空间System.Collection.Generic Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键必须是 ...
- (转载)[FFmpeg]使用ffmpeg从各种视频文件中直接截取视频图片
你曾想过从一个视频文件中提取图片吗?在Linux下就可以,在这个教程中我将使用ffmpeg来从视频中获取图片. 什么是ffmpeg?What is ffmpeg? ffmpeg是一个非常有用的命令行程 ...
- MySQL性能测试工具之mysqlslap使用详解
mysqlslap是mysql自带的基准测试工具,优点:查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新,给出了性能测试数据而且提供了多种引擎的性能比较.msq ...
- 分享整理的sql脚本
1. 表空间使用率 SQL> select a.tablespace_name, 2 round(a.total_size) "total_size M" ...
- 制作SSL证书
上一节介绍了OpenSSL的目录结构,本节介绍一下SSL证书的制作. OpenSSL安装后建议把其中的bin目录添加到系统环境变量中,方便以后操作. 建立一个新的目录SSL专门用来制作证书. 建立证书 ...
- Core Animation系列之CADisplayLink(转)
转自 http://www.tuicool.com/articles/meMVR3 一直以来都想好好学习下CoreAnimation,奈何涉及的东西太多,想要一次性全部搞定时间上不允许,以后会断断续续 ...
- MYSQL命令行连接数据库
连接数据库 mysql -uroot -proot -P3306 -Dmydemo # 参数详解 -uuser 用户user -ppwd 密码 -P3306 端口 -Dmysql 数据库 #显示所有数 ...
- 平衡搜索树(一) AVL树
AVL树 AVL树又称为高度平衡的二叉搜索树,是1962年有俄罗斯的数学家G.M.Adel'son-Vel'skii和E.M.Landis提出来的.它能保持二叉树的高度 平衡,尽量降低二叉树的高度,减 ...
- POJ 3468.A Simple Problem with Integers 解题报告
用树状数组和线段树会比较简单,这里用这道题来学习Splay. 第一次写,代码比较丑 /* 初始化添加一个key值足够大的结点 保证每个需要的结点都有后继 */ #include <iostrea ...
- 用urlencode(String str)对URL传递参数进行编码,提高安全
在PHP 提交地址后面带有参数的时候,参数会在浏览器的地址栏暴露无疑,这样是不安全的,这个时候就必须用些方法对这些参数进行安全处理 这里可以用 urlencode(String URL);//对URL ...