class Solution:
def isOneEditDistance(self, s: str, t: str) -> bool:
m, n = len(s), len(t)
# Ensure s is the shorter string
if m > n:
return self.isOneEditDistance(t, s)
# Length difference > 1 means more than one edit
if n - m > 1:
return False
for i in range(m):
if s[i] != t[i]:
if m == n:
# Replace operation
return s[i+1:] == t[i+1:]
else:
# Insert operation in s or delete in t
return s[i:] == t[i+1:]
# All previous chars are the same, last char can be extra in t
return n - m == 1
class Solution {
public:
bool isOneEditDistance(string s, string t) {
int m = s.size(), n = t.size();
if (m > n) return isOneEditDistance(t, s);
if (n - m > 1) return false;
for (int i = 0; i < m; ++i) {
if (s[i] != t[i]) {
if (m == n)
return s.substr(i+1) == t.substr(i+1);
else
return s.substr(i) == t.substr(i+1);
}
}
return n - m == 1;
}
};
class Solution {
public boolean isOneEditDistance(String s, String t) {
int m = s.length(), n = t.length();
if (m > n) return isOneEditDistance(t, s);
if (n - m > 1) return false;
for (int i = 0; i < m; i++) {
if (s.charAt(i) != t.charAt(i)) {
if (m == n)
return s.substring(i+1).equals(t.substring(i+1));
else
return s.substring(i).equals(t.substring(i+1));
}
}
return n - m == 1;
}
}
var isOneEditDistance = function(s, t) {
let m = s.length, n = t.length;
if (m > n) return isOneEditDistance(t, s);
if (n - m > 1) return false;
for (let i = 0; i < m; i++) {
if (s[i] !== t[i]) {
if (m === n)
return s.substring(i+1) === t.substring(i+1);
else
return s.substring(i) === t.substring(i+1);
}
}
return n - m === 1;
};
Given two strings, s
and t
, determine if they are exactly one edit distance apart. An edit is defined as one of the following operations:
s
to get t
s
to get t
s
to get t
The function should return true
if you can transform s
into t
with exactly one edit, and false
otherwise.
Constraints:
s
and t
are non-null strings (may be empty).false
.
To solve this problem, we first consider the definition of "one edit distance." The two strings must differ by exactly one simple operation: insert, delete, or replace a single character. If the strings are identical, then zero edits are needed, so we return false
. If the length difference is more than one, it's impossible to make them match with a single edit.
Brute force might suggest generating all possible one-edit variations of s
and checking if t
is among them, but this is inefficient, especially for longer strings. Instead, we can compare the strings character by character and identify the first difference. At that point, we can check if a single operation would make the rest of the strings match.
We also notice that the three operations can be unified by always comparing the shorter string to the longer one. This allows us to handle insertion and deletion as two sides of the same coin.
s
and t
. Always treat s
as the shorter or equal-length string. If not, swap them.
false
because more than one edit would be needed.
s
(the shorter string). For each character, compare it with the corresponding character in t
.
t
is longer by one, check if the substring of s
from this index matches the substring of t
from the next index. This simulates an insert in s
or a delete in t
.t
has one extra character at the end.
This approach only requires a single pass through the strings and uses constant extra space.
Consider s = "cat" and t = "cats":
m = 3
, n = 4
. n - m = 1
, so possible one edit.t
is longer by one character, and all previous characters matched, the only difference is the last character 's'
in t
.s
and t
are one edit distance apart (insertion at the end).Now, consider s = "cat" and t = "cut":
s
and comparing to t
is O(N2), where N is the length of the string, because for each position you could insert, delete, or replace a character, and each operation requires copying the string.
The "One Edit Distance" problem is efficiently solved by comparing the two strings character by character and handling the three possible edit operations (insert, delete, replace) in a unified way. By always treating the shorter string as s
and checking for a single difference, the solution avoids unnecessary computation and achieves linear time with constant space. The approach is elegant because it reduces the problem to a simple scan and comparison, making it both intuitive and performant.