One Edit Distance - Solution & Explanation
Problem Statement
Given two strings s and t, return true if they are both one edit distance apart, otherwise return false.
A string s is said to be one distance apart from a string t if you can:
- Insert exactly one character into
sto gett. - Delete exactly one character from
sto gett. - Replace exactly one character of
swith a different character to gett.
Example 1:
Input: s = "ab", t = "acb" Output: true Explanation: We can insert 'c' into s to get t.
Example 2:
Input: s = "", t = "" Output: false Explanation: We cannot get t from s by only one step.
Constraints:
0 <= s.length, t.length <= 104sandtconsist of lowercase letters, uppercase letters, and digits.
Approach Overview
Problem Overview: You are given two strings s and t. The goal is to determine whether they are exactly one edit apart. An edit can be an insertion, deletion, or replacement of a single character. If zero edits or more than one edit are required, the answer should be false.
Approach 1: Edit Distance Dynamic Programming (O(m*n) time, O(m*n) space)
A straightforward solution computes the classic edit distance between the two strings using dynamic programming. Create a DP table where dp[i][j] represents the minimum number of edits required to convert the first i characters of s into the first j characters of t. Each state considers insertion, deletion, or replacement. After filling the table, check whether the final edit distance equals exactly 1. This approach is simple and reusable for general edit distance problems, but it performs unnecessary work because the problem only cares about distance one. Time complexity is O(m*n) and space complexity is O(m*n), where m and n are the string lengths.
Approach 2: Discuss Different Cases with Two Pointers (O(n) time, O(1) space)
The optimal solution relies on careful case analysis and a linear scan using two pointers. First check the length difference. If the absolute difference exceeds one, the strings cannot be one edit apart. Otherwise iterate through both strings until the first mismatch appears. At that point three scenarios are possible: replacement when the lengths are equal, insertion into the shorter string, or deletion from the longer string. Instead of performing an explicit edit, advance the pointers according to the case and verify that the remaining substrings match.
This approach works because only one edit is allowed. Once a mismatch occurs, the rest of the characters must align perfectly. If no mismatch appears during the scan, the strings are one edit apart only when their lengths differ by exactly one (an extra trailing character). The algorithm processes each character once, giving O(n) time complexity and constant O(1) space. The logic relies heavily on careful pointer movement and efficient string comparison.
Recommended for interviews: Interviewers expect the linear two-pointer approach. The dynamic programming method demonstrates understanding of general edit distance, but it is overkill for this constraint. Showing the DP idea briefly and then optimizing to the case-based two pointer scan signals strong problem-solving ability and awareness of time complexity tradeoffs.
Solution
Let m represent the length of string s, and n represent the length of string t. We can assume that m is always greater than or equal to n.
If m-n > 1, return false directly;
Otherwise, iterate through s and t, if s[i] is not equal to t[i]:
- If
m neq n, compares[i+1:]witht[i:], return true if they are equal, otherwise return false; - If
m = n, compares[i:]witht[i:], return true if they are equal, otherwise return false.
If the iteration ends, it means that all the characters of s and t that have been iterated are equal, at this time it needs to satisfy m=n+1.
The time complexity is O(m), where m is the length of string s. The space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Edit Distance Dynamic Programming | O(m*n) | O(m*n) | When solving the general edit distance problem or when multiple edits must be computed |
| Case Analysis with Two Pointers | O(n) | O(1) | Best for checking if two strings differ by exactly one edit |
Video Solution
161. One Edit Distance (LeetCode) • hakunamatasq • 2,580 views views
Watch 9 more video solutions →Frequently Asked Questions
Is One Edit Distance easy or hard?
One Edit Distance Python/Java solution
How to solve One Edit Distance in O(n)?
What is the best approach for One Edit Distance?
Is One Edit Distance asked at Google/Amazon/Meta?
What data structure is used in One Edit Distance?
What is the time complexity of One Edit Distance?
Ready to solve this problem?
Practice One Edit Distance with our built-in code editor and test cases.
Practice on FleetCode