Skip to content

Commit 47cbcd5

Browse files
refactor 161
1 parent b74d7c6 commit 47cbcd5

File tree

1 file changed

+37
-39
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+37
-39
lines changed

src/main/java/com/fishercoder/solutions/_161.java

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
* Given two strings s and t, determine if they are both one edit distance apart.
77
*
88
* Note:
9-
*
109
* 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
1513
*
1614
* Example 1:
1715
* Input: s = "ab", t = "acb"
@@ -29,43 +27,43 @@
2927
* Explanation: We can replace '0' with '1' to get t.
3028
*/
3129
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();
3634

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

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+
}
6366
return false;
64-
}
6567
}
66-
return diffCnt == 1;
67-
}
68-
return false;
6968
}
70-
}
7169
}

0 commit comments

Comments
 (0)