public class Solution {
public boolean isOneEditDistance(String s, String t) {
int sl = s.length(), tl = t.length();
if (sl == tl) {
return isModified(s, t);
}
if (sl - tl == 1) {
return isDeleted(s, t);
}
if (tl - sl == 1) {
return isDeleted(t, s);
}
return false;
}
private boolean isModified(String s, String t) {
boolean modified = false;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != t.charAt(i)) {
if (modified) {
return false;
}
modified = true;
}
}
return modified;
}
private boolean isDeleted(String s, String t) {
int i = 0;
for (; i < t.length(); i++) {
if (s.charAt(i) != t.charAt(i)) {
break;
}
}
return s.substring(i + 1).equals(t.substring(i));
}
}
One Edit Distance
最新推荐文章于 2019-04-24 11:24:31 发布