Given two strings S and T, determine if they are both one edit distance apart.
public boolean isOneEditDistance(String s, String t) {
if (Math.abs(s.length() - t.length()) > 1) {
return false;
}
if (s.length() > t.length()) {
return isOneEditDistance(t, s);
}
int i = 0;
int shift = t.length() - s.length();
while (i < s.length() && s.charAt(i) == t.charAt(i)) {
i++;
}
if (i == s.length()) {
return shift > 0;
}
if (shift == 0) {
i++;
}
while (i < s.length() && s.charAt(i) == t.charAt(i+shift)) {
i++;
}
return i == s.length();
}