Mid/Senior

One Edit Distance

Given two strings s and t, return true if they are exactly one edit distance apart; otherwise, return false.

One edit is defined as exactly one of the following operations performed on s:

  • Insert one character.
  • Delete one character.
  • Replace one character with another character.

The strings must differ by exactly one edit, so two identical strings are not considered one edit distance apart.

Example 1
Inputs = "ab", t = "acb"
Outputtrue
Inserting the character c into s produces t.
Example 2
Inputs = "", t = ""
Outputfalse
The strings are identical, so they are zero edits apart, not one edit apart.

Constraints

  • 0 <= s.length, t.length <= 10^4
  • s and t consist of lowercase letters, uppercase letters, and digits

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate