forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
30 lines (30 loc) · 820 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public boolean oneEditAway(String first, String second) {
int m = first.length(), n = second.length();
if (m < n) {
return oneEditAway(second, first);
}
if (m - n > 1) {
return false;
}
int cnt = 0;
if (m == n) {
for (int i = 0; i < n; ++i) {
if (first.charAt(i) != second.charAt(i)) {
if (++cnt > 1) {
return false;
}
}
}
return true;
}
for (int i = 0, j = 0; i < m; ++i) {
if (j == n || (j < n && first.charAt(i) != second.charAt(j))) {
++cnt;
} else {
++j;
}
}
return cnt < 2;
}
}