题目:

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. VMware下LINUX的虚拟机增加磁盘空间

    先关闭虚拟机电源,做如下设置:“ 虚拟机”--“虚拟机设置”--“磁盘”--“扩展” 可以随意添加你需要增到到的磁盘大小(如15Gb,表示磁盘总量,包含原来的磁盘容量); 再重启电源进入系统做如下步骤 ...

  2. 未完待续的JAVA基础知识

    第二卷 1.每个JAVA程序必须有一个main函数,但并非是每个类都有,main函数必须声明为static函数. 2.println与print之间的区别是换行与不换行. 3.在JAVA中,不想C/C ...

  3. items 与iteritems

    dict的items函数返回的是键值对的元组的列表,而iteritems使用的是键值对的generator. items当使用时会调用整个列表 iteritems当使用时只会调用值. >> ...

  4. 谈谈asp.net中的<% %>,<%= %>,<%# %><%$ %>的使用

    学而不思则罔,思而不学则殆,每天坚持一小步,则成功一大步 asp.net中的<% %>,<%= %>,<%#eval("") %><%$ ...

  5. HTML5标签及使用方法描述

    HTML 5 作为新一代的超文本标记语言,增加了许多标签.这些标签不但更有语义,而且功能强大.具体有以下标签: <article> 定义外部的内容.比如来自一个外部的新闻提供者的一篇新的文 ...

  6. (十一)Hibernate 高级配置

    第一节:配置数据库连接池 反问数据库,需要不断的创建和释放连接,假如访问量大的话,效率比较低级,服务器消耗大: 使用数据库连接池,我们可以根据实际项目的情况,定义连接池的连接个数,从而可以实现从连接池 ...

  7. Mysql的权限管理

    权限管理 创建用户 语法:     create user '用户名'[@'主机名'][identified by '密码']; 示例: 说明:     用户名必须使用引号     '主机名'可以是以 ...

  8. POJ 1276 Cash Machine -- 动态规划(背包问题)

    题目地址:http://poj.org/problem?id=1276 Description A Bank plans to install a machine for cash withdrawa ...

  9. dialog的传值

    A页面 等待B页面返回值的textBox:<asp:TextBox ID="TextBox1" runat="server"></asp:Te ...

  10. 使用thinkPHP实现数据更新一例【原创】

    在上一篇文章中我们实现了数据的删除和批量删除,这一篇文章我们将实现数据的更新. 首先依然是预期效果图: 点击修改后进入modi.html页面,然后进行修改,如此处修改了真实姓名这一属性: 点击保存: ...