One Edit Away Check

Hard ⏱ 18 min 29% acceptance ★★★★★ 4.7
Write one_edit_away(s1, s2) that returns True if s1 and s2 differ by at most one single-character edit — an insertion, a deletion, or a replacement.

Examples

Example 1
Input
s1 = 'pale', s2 = 'ple'
Output
True
Explanation

Deleting the 'a' from 'pale' gives 'ple'.

Example 2
Input
s1 = 'pales', s2 = 'pale'
Output
True
Explanation

One trailing character inserted/deleted.

Example 3
Input
s1 = 'pale', s2 = 'bale'
Output
True
Explanation

One character replaced ('p' -> 'b').

Example 4
Input
s1 = 'pale', s2 = 'bake'
Output
False
Explanation

Two characters differ, which is more than one edit.

Constraints

  • 0 <= len(s1), len(s2) <= 10^4

Topics

StringsComparison

Companies

GoogleAppleMeta

Hints

Hint 1

If the length difference is more than 1, the answer is immediately False.

Hint 2

Equal lengths: count mismatched positions; at most 1 is OK.

Hint 3

Lengths differing by 1: walk both strings with two pointers, allowing exactly one skip in the longer string when a mismatch is hit.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.