One Edit Distance
Example
Input:
s = "ab",
t = "acb"
Output: true
Explanation:
We can insert 'c' into s
to get t.Note
Code
Last updated
Input:
s = "ab",
t = "acb"
Output: true
Explanation:
We can insert 'c' into s
to get t.Last updated
Input:
s = "cab",
t = "ad"
Output:
false
Explanation:
We cannot get t from s by only one step.Input:
s = "1203",
t = "1213"
Output:
true
Explanation:
We can replace '0' with '1' to get t.Math.abs(s.length() - t.length()) == 1;public boolean isOneEditDistance(String s, String t) {
for (int i = 0; i < Math.min(s.length(), t.length()); i++) {
if (s.charAt(i) != t.charAt(i)) {
if (s.length() == t.length()) {
return s.substring(i + 1).equals(t.substring(i + 1));
} else if (s.length() > t.length()) {
return s.substring(i + 1).equals(t.substring(i));
} else {
return s.substring(i).equals(t.substring(i + 1));
}
}
}
return Math.abs(s.length() - t.length()) == 1;
}