1. 程式人生 > >161. One Edit Distance

161. One Edit Distance

and ref pytho amp with lan diff ase test case

題目:

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

161. One Edit Distance