1
1
package com .fishercoder .solutions ;
2
2
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
- * There are 3 possiblities to satisify one edit distance apart:
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
13
- *
14
- * Example 1:
15
- * Input: s = "ab", t = "acb"
16
- * Output: true
17
- * Explanation: We can insert 'c' into s to get t.
18
- *
19
- * Example 2:
20
- * Input: s = "cab", t = "ad"
21
- * Output: false
22
- * Explanation: We cannot get t from s by only one step.
23
- *
24
- * Example 3:
25
- * Input: s = "1203", t = "1213"
26
- * Output: true
27
- * Explanation: We can replace '0' with '1' to get t.
28
- */
29
3
public class _161 {
30
4
public static class Solution1 {
31
5
public boolean isOneEditDistance (String s , String t ) {
@@ -48,9 +22,8 @@ public boolean isOneEditDistance(String s, String t) {
48
22
j ++;
49
23
}
50
24
}
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
25
+ return diffCnt == 1 || diffCnt == 0 ;
26
+ //it could be the last char of the longer is the different one, in that case, diffCnt remains to be zero
54
27
} else if (s .length () == t .length ()) {
55
28
int diffCnt = 0 ;
56
29
for (int i = 0 ; i < s .length (); i ++) {
0 commit comments