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/

6/14/2017

2ms, 58%

提交好几次才通过,所以代码像打补丁一样。注意的test case:

2个空string -- false

相同的string -- false

当检查出来不一致时,根据长度来比较之后的substring

 1 public class Solution {
 2     public boolean isOneEditDistance(String s, String t) {
 3         if (s == null || t == null) {
 4             return false;
 5         }
 6         int sLen = s.length();
 7         int tLen = t.length();
 8         if (sLen == 0 && tLen == 0) {
 9             return false;
10         }
11         if (sLen == 0 && tLen == 1 || sLen == 1 && tLen == 0) {
12             return true;
13         }
14         if (Math.abs(sLen - tLen) > 1) {
15             return false;
16         }
17         int i = 0, j = 0;
18         while (i < sLen && j < tLen) {
19             char c = s.charAt(i);
20             char d = t.charAt(j);
21             if (c != d) {
22                 break;
23             }
24             i++;
25             j++;
26         }
27         if (i == sLen && j == tLen) {
28             return false;
29         }
30         if (sLen == tLen) {
31             return s.substring(i + 1, sLen).equals(t.substring(j + 1, tLen));
32         } else if (sLen < tLen) {
33             return s.substring(i, sLen).equals(t.substring(j + 1, tLen));
34         } else {
35             return s.substring(i + 1, sLen).equals(t.substring(j, tLen));
36         }
37     }
38 }

别人的做法,

跟我类似,也是利用了后面的substring,不过前期省很多

https://discuss.leetcode.com/topic/30308/my-clear-java-solution-with-explanation

 1 public boolean isOneEditDistance(String s, String t) {
 2     for (int i = 0; i < Math.min(s.length(), t.length()); i++) { 
 3         if (s.charAt(i) != t.charAt(i)) {
 4             if (s.length() == t.length()) // s has the same length as t, so the only possibility is replacing one char in s and t
 5                 return s.substring(i + 1).equals(t.substring(i + 1));
 6             else if (s.length() < t.length()) // t is longer than s, so the only possibility is deleting one char from t
 7                 return s.substring(i).equals(t.substring(i + 1));                
 8             else // s is longer than t, so the only possibility is deleting one char from s
 9                 return t.substring(i).equals(s.substring(i + 1));
10         }
11     }       
12     //All previous chars are the same, the only possibility is deleting the end char in the longer one of s and t 
13     return Math.abs(s.length() - t.length()) == 1;        
14 }

另外一种

 1 public class Solution {
 2     public boolean isOneEditDistance(String s, String t) {
 3         if (s == null || t == null || s.equals(t) || Math.abs(s.length() - t.length()) > 1) return false;
 4         if (s.length() > t.length()) return isOneEditDistance(t, s);
 5         boolean hasDiff = false;
 6         for (int i = 0, j = 0; i < s.length(); i++, j++) {
 7             if (s.charAt(i) != t.charAt(j)) {
 8                 if (hasDiff) return false;
 9                 hasDiff = true;
10                 if (s.length() < t.length()) i--;
11             }
12         }
13         return true;
14     }
15 }

2个指针

https://discuss.leetcode.com/topic/27379/java-python-two-pointer-solution

更多讨论

https://discuss.leetcode.com/category/169/one-edit-distance

原文地址:https://www.cnblogs.com/panini/p/7010770.html