|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/**Given two strings S and T, determine if they are both one edit distance apart.*/ |
| 3 | +/** |
| 4 | + * 161. One Edit Distance |
| 5 | + * |
| 6 | + * Given two strings s and t, determine if they are both one edit distance apart. |
| 7 | + * |
| 8 | + * Note: |
| 9 | + * |
| 10 | + * 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 |
| 15 | + * |
| 16 | + * Example 1: |
| 17 | + * Input: s = "ab", t = "acb" |
| 18 | + * Output: true |
| 19 | + * Explanation: We can insert 'c' into s to get t. |
| 20 | + * |
| 21 | + * Example 2: |
| 22 | + * Input: s = "cab", t = "ad" |
| 23 | + * Output: false |
| 24 | + * Explanation: We cannot get t from s by only one step. |
| 25 | + * |
| 26 | + * Example 3: |
| 27 | + * Input: s = "1203", t = "1213" |
| 28 | + * Output: true |
| 29 | + * Explanation: We can replace '0' with '1' to get t. |
| 30 | + */ |
4 | 31 | 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(); |
5 | 36 |
|
6 |
| - public static boolean isOneEditDistance(String s, String t) { |
7 |
| - char[] schar = s.toCharArray(); |
8 |
| - char[] tchar = t.toCharArray(); |
| 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; |
9 | 40 |
|
10 |
| - if (Math.abs(s.length() - t.length()) == 1) { |
11 |
| - char[] longer = (s.length() > t.length()) ? schar : tchar; |
12 |
| - char[] shorter = (longer == schar) ? tchar : schar; |
13 |
| - |
14 |
| - int diffCnt = 0; |
15 |
| - int i = 0; |
16 |
| - int j = 0; |
17 |
| - for (; i < shorter.length && j < longer.length; ) { |
18 |
| - if (longer[j] != shorter[i]) { |
19 |
| - diffCnt++; |
20 |
| - j++; |
21 |
| - } else { |
22 |
| - i++; |
23 |
| - j++; |
24 |
| - } |
25 |
| - } |
26 |
| - return diffCnt == 1 || diffCnt == 0;//it could be the last char of the longer is the different one, in that case, diffCnt remains to be zero |
27 |
| - } else if (s.length() == t.length()) { |
28 |
| - int diffCnt = 0; |
29 |
| - for (int i = 0; i < s.length(); i++) { |
30 |
| - if (schar[i] != tchar[i]) { |
31 |
| - diffCnt++; |
32 |
| - } |
33 |
| - if (diffCnt > 1) { |
34 |
| - return false; |
35 |
| - } |
36 |
| - } |
37 |
| - return diffCnt == 1; |
| 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 | + } |
38 | 52 | }
|
39 |
| - return false; |
40 |
| - } |
41 |
| - |
42 |
| - public static void main(String... strings) { |
43 |
| - String s = "a"; |
44 |
| - String t = "ac"; |
45 |
| - System.out.println(isOneEditDistance(s, t)); |
| 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) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + } |
| 66 | + return diffCnt == 1; |
| 67 | + } |
| 68 | + return false; |
46 | 69 | }
|
47 |
| - |
| 70 | + } |
48 | 71 | }
|
0 commit comments