161. One Edit Distance
题目:
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的更多相关文章
- [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 ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
- [LeetCode#161] One Edit Distance
Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...
- 【LeetCode】161. One Edit Distance
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- Android四大组件之BroadcastReceiver
什么是BroadcastReceiver? BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在Android系统中,广播体现在方方面面, ...
- 误解了Windows Server AppFabric
想为自己的流程引擎找一个宿主,选择了几套方案,想先从AppFabric开始,原因主要出于以下几点: 1. 自己用过Windows Service或Form作为一些定时任务等应用的宿主,但苦于学艺不精, ...
- SQL Server高级内容之表表达式和复习
1. 表表达式 (1) 将表作为一个源或将查询的一个结果集作为一个源,对源做处理,然后得到一个新的数据源,对其进行查询. (2)表表达式放在from子句中 (3)派生表,将表的查询得到的结果集作为一 ...
- 获取或设置checkbox radio select的值
单选: 获取值:$("input[name='rdo']:checked").val(); 设置值:$("input[name='rdo'][value='3']&quo ...
- [01] Oracle数据库简介
Oracle关系型数据库:建立在关系模型上. Oracle10g:g(grid)网格技术,网格计算(Grid Computing)通过网络共享,将大量的计算机连接起来,联合各个计算机的多余处理能力,产 ...
- NSdata 与 NSString,Byte数组,UIImage 的相互转换
1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adataen ...
- 详解Windows 7系统中IE8/IE9/IE10三个版本的关系(转)
今年(2013)年初,微软开放了姗姗来迟的 IE10 for Windows 7 版本下载.至此,Windows 7 平台上可以运行三个 IE 浏览器版本.虽然 Windows 与 IE 经历了诸多版 ...
- hibernate的dao操作不能提交到数据库问题的解决
刚学的时候总是各种错误,解决方法也无厘头的很 将UserDAO里面的的save方法修改try { getSession().save(transientInstance); log.debug(&qu ...
- 常用Linux/Unix/Mac Os命令
常用Linux/Unix/Mac OS命令 参考: 1.50 Most Frequently Used UNIX / Linux Commands (With Examples)
- DLL详解及Denpendcy Walker的使用
下面的文章被N次转载,为了尊重原作,\(^o^)/~,贴出最早发布这篇文章的地址及作者. 动态链接库 Windows的活动大陆 2006-07-26 09:21 作者:狂ρκ来源:电脑爱好者 在 ...