|
6 | 6 | * Given two strings s and t, determine if they are both one edit distance apart.
|
7 | 7 | *
|
8 | 8 | * Note:
|
9 |
| - * |
10 | 9 | * There are 3 possiblities to satisify one edit distance apart:
|
11 |
| - * |
12 |
| - * Insert a character into s to get t |
13 |
| - * Delete a character from s to get t |
14 |
| - * Replace a character of s to get t |
| 10 | + * Insert a character into s to get t |
| 11 | + * Delete a character from s to get t |
| 12 | + * Replace a character of s to get t |
15 | 13 | *
|
16 | 14 | * Example 1:
|
17 | 15 | * Input: s = "ab", t = "acb"
|
|
29 | 27 | * Explanation: We can replace '0' with '1' to get t.
|
30 | 28 | */
|
31 | 29 | public class _161 {
|
32 |
| - public static class Solution1 { |
33 |
| - public boolean isOneEditDistance(String s, String t) { |
34 |
| - char[] schar = s.toCharArray(); |
35 |
| - char[] tchar = t.toCharArray(); |
| 30 | + public static class Solution1 { |
| 31 | + public boolean isOneEditDistance(String s, String t) { |
| 32 | + char[] schar = s.toCharArray(); |
| 33 | + char[] tchar = t.toCharArray(); |
36 | 34 |
|
37 |
| - if (Math.abs(s.length() - t.length()) == 1) { |
38 |
| - char[] longer = (s.length() > t.length()) ? schar : tchar; |
39 |
| - char[] shorter = (longer == schar) ? tchar : schar; |
| 35 | + if (Math.abs(s.length() - t.length()) == 1) { |
| 36 | + char[] longer = (s.length() > t.length()) ? schar : tchar; |
| 37 | + char[] shorter = (longer == schar) ? tchar : schar; |
40 | 38 |
|
41 |
| - int diffCnt = 0; |
42 |
| - int i = 0; |
43 |
| - int j = 0; |
44 |
| - for (; i < shorter.length && j < longer.length; ) { |
45 |
| - if (longer[j] != shorter[i]) { |
46 |
| - diffCnt++; |
47 |
| - j++; |
48 |
| - } else { |
49 |
| - i++; |
50 |
| - j++; |
51 |
| - } |
52 |
| - } |
53 |
| - return diffCnt == 1 |
54 |
| - || diffCnt |
55 |
| - == 0;//it could be the last char of the longer is the different one, in that case, diffCnt remains to be zero |
56 |
| - } else if (s.length() == t.length()) { |
57 |
| - int diffCnt = 0; |
58 |
| - for (int i = 0; i < s.length(); i++) { |
59 |
| - if (schar[i] != tchar[i]) { |
60 |
| - diffCnt++; |
61 |
| - } |
62 |
| - if (diffCnt > 1) { |
| 39 | + int diffCnt = 0; |
| 40 | + int i = 0; |
| 41 | + int j = 0; |
| 42 | + for (; i < shorter.length && j < longer.length; ) { |
| 43 | + if (longer[j] != shorter[i]) { |
| 44 | + diffCnt++; |
| 45 | + j++; |
| 46 | + } else { |
| 47 | + i++; |
| 48 | + j++; |
| 49 | + } |
| 50 | + } |
| 51 | + return diffCnt == 1 |
| 52 | + || diffCnt |
| 53 | + == 0;//it could be the last char of the longer is the different one, in that case, diffCnt remains to be zero |
| 54 | + } else if (s.length() == t.length()) { |
| 55 | + int diffCnt = 0; |
| 56 | + for (int i = 0; i < s.length(); i++) { |
| 57 | + if (schar[i] != tchar[i]) { |
| 58 | + diffCnt++; |
| 59 | + } |
| 60 | + if (diffCnt > 1) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + } |
| 64 | + return diffCnt == 1; |
| 65 | + } |
63 | 66 | return false;
|
64 |
| - } |
65 | 67 | }
|
66 |
| - return diffCnt == 1; |
67 |
| - } |
68 |
| - return false; |
69 | 68 | }
|
70 |
| - } |
71 | 69 | }
|
0 commit comments