Thinking process
public class Solution {
public boolean isOneEditDistance(String s, String t) {
if (Math.abs(s.length() - t.length()) > 1) return false;
if (s.length() == t.length()) {
int diff = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) != t.charAt(i)) {
diff++;
}
}
if (diff == 1) return true;
} else {
return (s.length() > t.length()) ? isOneInsert(s, t) : isOneInsert(t, s);
}
return false;
}
private boolean isOneInsert (String s, String t) {
int diff = 0;
int i = 0, j = 0;
while (j < t.length() && i < s.length()) {
if (s.charAt(i) == t.charAt(j)) {
i++;
j++;
} else {
diff++;
i++;
}
}
return diff <= 1;
}
}