Ctrl K

Exactly One Edit Apart

Medium
You get two strings, first and second. An edit is one of these operations on first: - insert one character anywhere; - delete one character; - replace one character with a *different* one. Return true if second can be obtained from first with exactly one edit, and false otherwise. In particular, equal strings give false.
Example 1
first="plan", second="plain" → True

Insert i between a and n and you get exactly second.

Example 2
first="Code", second="code" → True

One replacement: C becomes c. Case matters.

Example 3
first="x7", second="x7" → False

The strings are already equal, but exactly one edit is required, so the answer is false.

Constraints
  • 0 ≤ len(first), len(second) ≤ 10⁴
  • The strings consist of upper- and lowercase English letters and digits
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Mirror Fragment MediumWords in Reverse Order MediumWhich Release Is Newer MediumReverse the Word Order in Place MediumPacking Runs in Place Medium
loading Python…
Input
first="plan", second="plain"
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 177 tests on the server, hidden ones included.