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 ...
随机推荐
- jsp(一) : servlet基础
1.客户 servlet容器 servlet 2.实现接口 #1.接口:servlet : constrontor .init().service.destroy ...
- powerdesign设置实体显示格式
工具-显示参数选择中,如下图:
- excel知识
excel中导出文本中的制表符去除方法:
- 禁止button响应回车(.net页面)
1. 深层次来说这不是 ASP.NET 的问题, 而是 html form 的 submit 按钮就是如何设计的. 当你的光标焦点进入某个表单元素的时候,会激活该表单中第一个(流布局顺从左到右,从上至 ...
- iOS开发——手机号,密码,邮箱,身份证号,中文判断
目前这些方面的判断主要是用了正则表达式 手机号的判断,目前主要是长度.均是数字,支持的号段由于第三方通讯比如京东通讯,小米通讯等支持的号段挺多, 有171,170,135,147等等,所以号段限制简单 ...
- C# ACM poj1003
这题很有内涵,先用简单方法 public static void acm1003(double a) { ) { return; } ; ) { / b; a = a - c; b++; } Cons ...
- Java实战之02Hibernate-04多表映射
十.多表映射 0.内容补充:数据完整性 作用:防止用户的误操作. 实体完整性:主键.用于确定表中唯一的一条记录. 域完整性:表中的字段. 数据类型约束: 非空约束: 唯一约束: 参照完整性: 多表设计 ...
- hdu 1715 大菲波数(高精度数)
Problem Description Fibonacci数列,定义如下: f(1)=f(2)=1 f(n)=f(n-1)+f(n-2) n>=3. 计算第n项Fibonacci数值. Inpu ...
- 九度OJ 1402 特殊的数 -- 位操作
题目地址:http://ac.jobdu.com/problem.php?pid=1402 题目描述: 现在有n个数,其中有一些出现了一次,一些出现了两次,一些出现了很多次.现在要求你找出那些只出现一 ...
- OpenJudge/Poj 2013 Symmetric Order
1.链接地址: http://bailian.openjudge.cn/practice/2013 http://poj.org/problem?id=2013 2.题目: Symmetric Ord ...