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

分析:https://segmentfault.com/a/1190000003906621

虽然我们可以用Edit Distance的解法,看distance是否为1,但Leetcode中会超时。这里我们可以利用只有一个不同的特点在O(N)时间内完成。如果两个字符串只有一个编辑距离,则只有两种情况:

  1. 两个字符串一样长的时候,说明有一个替换操作,我们只要看对应位置是不是只有一个字符不一样就行了

  2. 一个字符串比另一个长1,说明有个增加或删除操作,我们就找到第一个对应位置不一样的那个字符,如果较长字符串在那个字符之后的部分和较短字符串那个字符及之后的部分是一样的,则符合要求

  3. 如果两个字符串长度差距大于1,肯定不对

 public class Solution {
public boolean isOneEditDistance(String s, String t) {
int m = s.length(), n = t.length();
if(m == n) return isOneModified(s, t);
if(m - n == ) return isOneDeleted(s, t);
if(n - m == ) return isOneDeleted(t, s);
// 长度差距大于2直接返回false
return false;
} private boolean isOneModified(String s, String t){
boolean modified = false;
// 看是否只修改了一个字符
for(int i = ; i < s.length(); i++){
if(s.charAt(i) != t.charAt(i)){
if(modified) return false;
modified = true;
}
}
return modified;
} public boolean isOneDeleted(String longer, String shorter){
// 找到第一组不一样的字符,看后面是否一样
for(int i = ; i < shorter.length(); i++){
if(longer.charAt(i) != shorter.charAt(i)){
return longer.substring(i + ).equals(shorter.substring(i));
}
}
return true;
}
}

另一种方法也挺好的:http://www.programcreek.com/2014/05/leetcode-one-edit-distance-java/

 public boolean isOneEditDistance(String s, String t) {
if (s == null || t == null) return false; int m = s.length(), n = t.length();
if (Math.abs(m - n) > ) return false; int i = , j = , count = ;
while (i < m && j < n) {
if (s.charAt(i) == t.charAt(j)) {
i++;
j++;
} else {
count++;
if (count > ) {
return false;
} if (m > n) {
i++;
} else if (m < n) {
j++;
} else {
i++;
j++;
}
}
} if (i < m || j < n) {
count++;
} if (count == ) {
return true;
} return false;
}

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. 【leetcode】Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

随机推荐

  1. [Unity] Unity3D研究院编辑器之独立Inspector属性

    本文转自: http://www.xuanyusong.com/archives/3680雨松MOMO Unity提供了强大的Editor功能, 我们可以很轻易的在EditorGUI中绘制任意的属性. ...

  2. SSH+Ext+mysql快速开发

    一.需要知识点 1.SSH整合 2.EXT使用 以及深入细节点 二.小功能实现 1.Servlet实现校验码验证 2.首页布局实现 3.struts错误信息显示(struts标签使用) 4.首页整体布 ...

  3. Mount挂载命令使用方法

    语法: mount -t 类型 -o 挂接方式 源路径 目标路径     -t 详细选项: 光盘或光盘镜像:iso9660 DOS fat16文件系统:msdos Windows 9x fat32文件 ...

  4. base64 加密

    Base64 参考网站:http://zh.wikipedia.org/wiki/Base64 简介 是网络上使用最广泛的编码系统,能够将任何二进制数据,转换成只有 65 个字符组成的文本文件 a~z ...

  5. linux文档常见后缀名

    echo "Start bakup mdsoss Source code ..."_Name="templatecdr_src_"`date +%Y%m%d%H ...

  6. systemctl 取代 service

    要使用systemd, linux内核版本要高于: 2.6.39 systemctl的命令格式: systemctl 动作命令(如start stop restart status) 服务名称.ser ...

  7. 来聊聊apply和call

    今天在群里讨论的时候,看到有人问apply怎么使用,突然想起自己刚接触这个方法的时候,也是一样的摸不着头脑. 记得当时上网看了很多讲解,可实际用的时候还是感觉有些蒙蒙哒orz.... 后来想一想,也许 ...

  8. c#winform选择文件,文件夹,打开指定目录方法

    private void btnFile_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDia ...

  9. PlayMaker 学习笔记

    1.v1.7.8.3版本无法使用全局变量,原因是Assets\Plugins\PlayMaker下的Resources文件夹变成中文了,无法创建全局变量文件,手动创建一个Resources文件夹即可

  10. Nginx安装配置(转)

    Nginx 安装配置 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/ ...