题目:

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

链接: http://leetcode.com/problems/one-edit-distance/

题解:

求两个字符串是否只有1个Edit Distance。 看着这道题又想起了Edit Distance那道。不过这道题不需要用DP,只用设一个boolean变量hasEdited来逐字符判断就可以了。写法大都借鉴了曹神的代码。用短的string和长的比较,假如字符不同,则hasEdited为true,假如s比t短,则下标i退回1来继续比较insert / delete的case。否则比较的是replace。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public boolean isOneEditDistance(String s, String t) { // compare short string with long string
if(s == null || t == null)
return false;
if(s.length() > t.length())
return isOneEditDistance(t, s);
if(t.length() - s.length() > 1)
return false;
boolean hasEdited = false; for(int i = 0, j = 0; i < s.length(); i++, j++) { // detect if only 1 change need to be made
if(s.charAt(i) != t.charAt(j)) {
if(hasEdited)
return false;
hasEdited = true;
if(s.length() < t.length()) //if s.length() < t.length(), back up one letter and continue compare
i--;
}
} return hasEdited || (s.length() < t.length()); // (s.length() < t.length()) for insert case or delete case
}
}

Update:

把s.equals(t)的相等判断从尾部挪到头部了,这样尾部直接return true就可以了

public class Solution {
public boolean isOneEditDistance(String s, String t) {
if(s == null || t == null || s.equals(t))
return false;
if(s.length() > t.length())
return isOneEditDistance(t, s);
if(t.length() - s.length() > 1)
return false; boolean hasEdited = false; for(int i = 0, j = 0; i < s.length(); i++, j++) {
if(s.charAt(i) != t.charAt(j)) {
if(hasEdited)
return false;
hasEdited = true;
if(s.length() < t.length())
i--;
}
} return true;
}
}

二刷:

依然是曹神的解法。

Java:

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public boolean isOneEditDistance(String s, String t) {
if (s == null || t == null || s.equals(t) || Math.abs(s.length() - t.length()) > 1) return false;
if (s.length() > t.length()) return isOneEditDistance(t, s);
boolean hasDiff = false;
for (int i = 0, j = 0; i < s.length(); i++, j++) {
if (s.charAt(i) != t.charAt(j)) {
if (hasDiff) return false;
hasDiff = true;
if (s.length() < t.length()) i--;
}
}
return true;
}
}

161. One Edit Distance的更多相关文章

  1. [LeetCode] 161. One Edit Distance 一个编辑距离

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  2. ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...

  3. [LeetCode#161] One Edit Distance

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

  4. 【LeetCode】161. One Edit Distance

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...

  5. [leetcode]161. One Edit Distance编辑步数为一

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  6. [LC] 161. One Edit Distance

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  7. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  8. [LeetCode] Edit Distance 编辑距离

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

  9. Edit Distance

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

随机推荐

  1. hhgis驱动

    function loadPathInfo 在car.js中 function loadPathInfo(carid, fnClass) { var qureydrive; if (jsonp) qu ...

  2. 再议Unity 3D

    一年前,偶发冲动,翻译了<[译] Unity3D游戏和facebook绑定(1:简介)>系列文章. 现在看有2个明显的好处, 一:给这个不温不火的博客带了top 3的人气: 二:我个人由此 ...

  3. cocos2d-x实战 C++卷 学习笔记--第6章 场景与层

    前言: 一个场景(Scene)是由多个层(Layer)组成,而且层的个数要至少是1,不能为0. 场景切换 场景切换相关函数 1)void  runWithScene(Scene*  scene) 该函 ...

  4. net下 Mysql Linq的使用, 更新数据,增加数据,删除数据

    net下访问mysql主要有2种方法: 1.字符串拼接访问 a.mysql官网下载并安装mysql-connector-net. b项目中引用mysql.data等 所有增删改查可以通过拼接sql语句 ...

  5. 关于css中的align-content属性详解

    align-content 作用: 会设置自由盒内部各个项目在垂直方向排列方式. 条件:必须对父元素设置自由盒属性display:flex;,并且设置排列方式为横向排列flex-direction:r ...

  6. 理解css 中的position五个属性

    在实际开发页面布局时,运用position,对定位的块级元素的嵌套的效果总是不太理解,这里做了几个测试 一般的在w3c中我们可以很容易的获取定义: static : 默认值.没有定位,元素出现在正常的 ...

  7. Java实战之02Hibernate-06处理并发

    十三.处理并发 1.事务的隔离级别 不考虑隔离级别出现的问题: 脏读:一个线程中的事务读到了另外一个线程中未提交的数据. 不可重复读:一个线程中的事务读到了另外一个线程中提交的update(更新)的数 ...

  8. android开发 单击按钮 实现页面间的跳转

    我的MainActivity.java部分代码 public class MainActivity extends ActionBarActivity { //不要定义button类型,会出错 Vie ...

  9. ASP.net后台弹出消息对话框的方法!【转】

          在winform后台,我们通过MessageBox.show(“消息")的方式来返回后台信息,在webform后台,我们通过Response.write(”消息")来返 ...

  10. 此博客不更新文章,请到www.xiaoxiangyucuo.com看最新文章

    请到www.xiaoxiangyucuo.com看更多资料,包括Linux,JavaScript,Python,MySQL,PHP,HTML,Photoshop,以及各类软件下载. 希望大家支持,提出 ...