161. One Edit Distance
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method Explanation
The problem asks whether two strings, s and t, are one edit distance apart. This means we can transform s into t by performing exactly one of the following operations: insertion, deletion, or replacement of a single character.
Solution
One Edit Distance in Python
class Solution:
def isOneEditDistance(self, s: str, t: str) -> bool:
n, m = len(s), len(t)
# Ensure that s is the shorter string to simplify logic.
if n > m:
return self.isOneEditDistance(t, s)
# The difference in lengths cannot be more than 1.
if m - n > 1:
return False
# Case 1: Same length strings (potential replacement).
if n == m:
diffs = 0
for i in range(n):
if s[i] != t[i]:
diffs += 1
# Exactly one replacement is allowed.
return diffs == 1
# Case 2: Length difference is 1 (potential insertion/deletion).
# Now we know m = n + 1.
for i in range(n):
if s[i] != t[i]:
# If a character differs, the rest of s must match the rest of t starting from the next character.
# This corresponds to an insertion in s at index i.
return s[i:] == t[i+1:]
# If no difference is found in the loop, it means s is a prefix of t.
# Since t is one character longer, this is a valid one-edit (insertion at the end).
return True
One Edit Distance in Java
class Solution {
public boolean isOneEditDistance(String s, String t) {
int n = s.length();
int m = t.length();
// Ensure that s is the shorter string to simplify logic.
if (n > m) {
return isOneEditDistance(t, s);
}
// The difference in lengths cannot be more than 1.
if (m - n > 1) {
return false;
}
// Case 1: Same length strings (potential replacement).
if (n == m) {
int diffs = 0;
for (int i = 0; i < n; i++) {
if (s.charAt(i) != t.charAt(i)) {
diffs++;
}
}
// Exactly one replacement is allowed.
return diffs == 1;
}
// Case 2: Length difference is 1 (potential insertion/deletion).
// Now we know m = n + 1.
for (int i = 0; i < n; i++) {
if (s.charAt(i) != t.charAt(i)) {
// If a character differs, the rest of s must match the rest of t starting from the next character.
// This corresponds to an insertion in s at index i.
return s.substring(i).equals(t.substring(i + 1));
}
}
// If no difference is found in the loop, it means s is a prefix of t.
// Since t is one character longer, this is a valid one-edit (insertion at the end).
return true;
}
}
One Edit Distance in C++
class Solution {
public:
bool isOneEditDistance(string s, string t) {
int n = s.length();
int m = t.length();
// Ensure that s is the shorter string to simplify logic.
if (n > m) {
return isOneEditDistance(t, s);
}
// The difference in lengths cannot be more than 1.
if (m - n > 1) {
return false;
}
// Case 1: Same length strings (potential replacement).
if (n == m) {
int diffs = 0;
for (int i = 0; i < n; ++i) {
if (s[i] != t[i]) {
diffs++;
}
}
// Exactly one replacement is allowed.
return diffs == 1;
}
// Case 2: Length difference is 1 (potential insertion/deletion).
// Now we know m = n + 1.
for (int i = 0; i < n; ++i) {
if (s[i] != t[i]) {
// If a character differs, the rest of s must match the rest of t starting from the next character.
// This corresponds to an insertion in s at index i.
return s.substr(i) == t.substr(i + 1);
}
}
// If no difference is found in the loop, it means s is a prefix of t.
// Since t is one character longer, this is a valid one-edit (insertion at the end).
return true;
}
};
One Edit Distance in TypeScript
function isOneEditDistance(s: string, t: string): boolean {
let n = s.length;
let m = t.length;
// Ensure that s is the shorter string to simplify logic.
if (n > m) {
return isOneEditDistance(t, s);
}
// The difference in lengths cannot be more than 1.
if (m - n > 1) {
return false;
}
// Case 1: Same length strings (potential replacement).
if (n === m) {
let diffs = 0;
for (let i = 0; i < n; i++) {
if (s[i] !== t[i]) {
diffs++;
}
}
// Exactly one replacement is allowed.
return diffs === 1;
}
// Case 2: Length difference is 1 (potential insertion/deletion).
// Now we know m = n + 1.
for (let i = 0; i < n; i++) {
if (s[i] !== t[i]) {
// If a character differs, the rest of s must match the rest of t starting from the next character.
// This corresponds to an insertion in s at index i.
return s.substring(i) === t.substring(i + 1);
}
}
// If no difference is found in the loop, it means s is a prefix of t.
// Since t is one character longer, this is a valid one-edit (insertion at the end).
return true;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.