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 ...
随机推荐
- php的标记形式
共三种: 推荐第一种,第三种需要在php.ini中配置 效果: 第三种配置 将short_open_tag=Off改为On重启Apache就可以了
- C#中的集合
[集合不同于数组,是一组可变类型的.可变数量的元素的组合,这些元素可能共享某些特征,需要以某种操作方式一起进行操作.一般来讲,为了便于操作这些元素的类型是相同的] [集合与数组的区别:数组是连续的.同 ...
- Classloaders and Classes
Classloaders and Classes (CLASSES) An example of the classloader (CLASSES) section that includes Cla ...
- DOM4J方式解析XML文件
dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory dom4j是一个简单的开源库,用于处理XML. X ...
- selenium文件上传的实现
一.对于上传文件, 从手动操作我们可以看出, 需要对window 窗体进行操作, 而对于selenium webdriver 在这方面应用就受到了限制. 但是, 庆幸的是, 对于含有input ele ...
- ios错误修改了系统头文件
一.打开终端 二.进入Xcode 输入命令: cd /Users/apple/Library/Developer/Xcode/ 三.打开当前 输入命令: open . 四.将DerivedData ...
- mongodb 数据备份,还原笔记
公司数据库迁移,所以补充了一下知识: 1 集合的导入和导出 命令行帮助 mongoexport --help 导出 导出 newsServer 数据库下 news 集合 mongoexport - ...
- 10_控制线程_线程让步yield
[线程让步yield()方法] yield()方法可以让当前正在执行的线程暂停,但它不会阻塞该线程,它只是将该线程从运行状态转入就绪状态. 只是让当前的线程暂停一下,让系统的线程调度器重新调度一次. ...
- Python Numpy
ebook on quant trading /量子交易 參考: Python在Windows下的安裝可以使用Python(x,y).
- VirtualBox single usermode boot
VirtualBox single usermode boot 当系统因为某些原因无法通过图形界面登录VirtualBox内的系统时,可以通过Grub进入命令行模式/单一用户界面模式. 参考: 1.R ...