forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneEditDistance.java
More file actions
25 lines (24 loc) · 702 Bytes
/
OneEditDistance.java
File metadata and controls
25 lines (24 loc) · 702 Bytes
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
public class OneEditDistance {
/**
* 最容易错的是结尾的条件sL != tL
*/
public boolean isOneEditDistance(String s, String t) {
int sL = s.length(), tL = t.length();
if (sL > tL) {
return isOneEditDistance(t, s);
}
if (tL - sL > 1) {
return false;
}
for (int i = 0; i < sL; i++) {
if (s.charAt(i) != t.charAt(i)) {
if (sL < tL) {
return s.substring(i).equals(t.substring(i + 1));
} else {
return s.substring(i + 1).equals(t.substring(i + 1));
}
}
}
return sL != tL;
}
}