This problem is related to the general Edit Distance problem (LeetCode #72), but with one simplification: we only need to check whether the edit distance is exactly one, not compute its actual value. That removes the need for dynamic programming.
There are only three one-edit scenarios. If the two strings have the same length, the only valid edit is a replacement, so exactly one character must differ. If the lengths differ by one, the only valid edit is an insertion or deletion (the same operation viewed from either string). If the lengths differ by more than one, no single edit can bridge the gap.
Comparing the two lengths tells us which case applies, and a single pass verifies the edit. One subtlety to keep in mind: two equal strings are zero edits apart, so identical inputs must return false.
0 <= s.length, t.length <= 10^4 → Full edit-distance dynamic programming is O(m*n), which builds a table of up to 10^8 cells. Because we only need to confirm a single edit, an O(n) length-based check avoids that table entirely.false.One way to solve this is to compute the full edit distance between s and t using dynamic programming, then check whether the result equals 1. This is the Levenshtein distance algorithm.
Build a 2D table where dp[i][j] is the minimum number of edits to convert the first i characters of s into the first j characters of t. Each cell depends on the three cells above, left, and diagonal, so we fill the table row by row and the answer ends up at dp[m][n].
This computes the entire edit distance when we only need to know whether it equals 1, which is more work than the problem requires.
m = len(s) and n = len(t).(m+1) x (n+1).dp[i][0] = i and dp[0][j] = j.s[i-1] == t[j-1], then dp[i][j] = dp[i-1][j-1]. Otherwise, dp[i][j] = 1 + min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]).dp[m][n] == 1.Loading animation...
The next approach drops the table. Because there are only three cases based on the length difference, each can be verified directly in a single pass.
If the edit distance between two strings is exactly one, the length difference alone narrows it to one of three cases:
false immediately.The algorithm checks the length difference to pick the case, then verifies it with one scan.
To keep the code to two branches, ensure s is the shorter or equal-length string by swapping when s is longer. That leaves only len(s) == len(t) (replace) and len(t) - len(s) == 1 (insert into s / delete from t).
Stopping at the first mismatch is safe because a single edit can only fix one divergence. Up to position i the two strings are identical, so the one allowed edit must be spent at i. In the replace case the edit consumes the mismatched pair, and s[i+1:] == t[i+1:] confirms no second divergence exists. In the insert/delete case the edit consumes the extra character in t, so the remaining check shifts t by one: s[i:] == t[i+1:]. If either comparison fails, a second edit would be required, so the answer is false.
The sLen != tLen check at the end covers the case where every character of s matches the prefix of t and the only difference is one extra character at the end of t (for example s = "ab", t = "abc"). The loop never finds a mismatch there, so the final return distinguishes that valid one-edit case from identical strings.
false.s is the shorter or equal-length string (swap if needed).t, then check that the rest matches.true only if the lengths differ by 1 (meaning the extra character is at the end of t).Loading animation...