Leetcode 161. One Edit Distance
来源:互联网 发布:windows启用网络发现 编辑:程序博客网 时间:2024/06/04 17:57
161. One Edit Distance
Total Accepted: 18099 Total Submissions: 61201 Difficulty: MediumGiven two strings S and T, determine if they are both one edit distance apart.
Hide Similar Problems
把人搞疯的一道题。。
思路:
本来直接按之前那个hard题目做,然后最后判断==1就行了。结果过不了大数据:
public class Solution { // 超时 public boolean isOneEditDistance(String s, String t) { if(s.length() == 0 && t.length() == 0) return false; if(s.length() == 0) return t.length() == 1; if(t.length() == 0) return s.length() == 1; char[] ss = s.toCharArray(); char[] tt = t.toCharArray(); int[][] dp = new int[ss.length + 1][tt.length + 1]; // dp[i][j] : s.substring(0, i) <-> t.substring(0, j) // initialize for(int i = 0; i < ss.length + 1; i++) dp[i][0] = i; for(int i = 0; i < tt.length + 1; i++) dp[0][i] = i; for(int i = 1; i < ss.length + 1; i++){ for(int j = 1; j < tt.length + 1; j++){ if(ss[i - 1] == tt[j -1]) dp[i][j] = dp[i - 1][j - 1]; else dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1; } } return dp[ss.length][tt.length] == 1; }}
于是如何提前判断就是问题:
if(i == j){if(dp[i][j] > 1) return false;}
118 / 130 test cases passed.
Status: Wrong Answer
Input:
"ab"
"cab"
Output:
false
Expected:
true
if(Math.abs(i - j) <= 1){if(dp[i][j] > 1) return false;}
111 / 130 test cases passed.
Status: Wrong Answer
Input:
"cb"
"ab"
Output:
false
Expected:
true
意思是不让DP咯
解法出自这里:
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; }}// 假如字符不同,则hasEdited为true,假如s比t短,则下标i退回1来继续比较insert / delete的case。否则比较的是replace。
另还有一讨论区解法,时间都是2ms:
public class Solution { public boolean isOneEditDistance(String s, String t) { int m = s.length(); int n = t.length(); if (Math.abs(m - n) > 1) { return false; } int len = Math.min(m, n); for (int i = 0; i < len; i++) { if (s.charAt(i) != t.charAt(i)) { boolean result = s.substring(i).equals(t.substring(i + 1)); result = result || s.substring(i + 1).equals(t.substring(i)); result = result || s.substring(i + 1).equals(t.substring(i + 1)); return result; } } return Math.abs(m - n) == 1; }}
0 0
- Leetcode-161.One Edit Distance
- [LeetCode]161. One Edit Distance
- LeetCode 161. One Edit Distance
- Leetcode 161. One Edit Distance
- LeetCode 161.One Edit Distance
- Leetcode One Edit Distance
- LeetCode - One Edit Distance
- leetcode - One Edit Distance
- #leetcode#One Edit Distance
- [LeetCode]One Edit Distance
- LeetCode—161. One Edit Distance
- [Leetcode] 161. One Edit Distance 解题报告
- 161. One Edit Distance
- 161. One Edit Distance
- 161. One Edit Distance
- leetcode 161: One Edit Distance
- LeetCode 161. One Edit Distance(编辑距离)
- LeetCode-161.One Edit Distance (JAVA)一次编辑距离
- Matlab预分配内存
- 配置Reporting Service 2012
- 欢迎使用CSDN-markdown编辑器
- 虚拟现实与增强现实
- 在Github和Git上fork之简单指南
- Leetcode 161. One Edit Distance
- 分布式配置管理平台Disconf
- Win8下用DOSBox编写汇编语言
- 取扑克牌问题
- java反射
- 第5周项目1 Raptor(1)
- bash脚本入门
- 如何计算混合IO理论性能
- C++Primer Plus 第二章编程练习个人答案