Skip to content

Commit b402748

Browse files
refactor 161
1 parent 3a2f437 commit b402748

File tree

2 files changed

+84
-40
lines changed

2 files changed

+84
-40
lines changed
Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,71 @@
11
package com.fishercoder.solutions;
22

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+
*/
431
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();
536

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;
940

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+
}
3852
}
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;
4669
}
47-
70+
}
4871
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._161;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _161Test {
10+
private static _161.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _161.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.isOneEditDistance("a", "ac"));
20+
}
21+
}

0 commit comments

Comments
 (0)