Skip to the code
cf.
+10 XP

#537 One Keystroke Away

easy

A form shows a gentle "did you mean" hint only when the typed value is very close to a saved value. Close means the two strings are equal, or one insert, one delete, or one replaced character turns one into the other.

Write isOneEditAway(typed, saved) that returns true if the strings are at most one edit apart, and false otherwise.

Example 1

Input: typed = "modal", saved = "model"
Output: true

Replace a with e.

Example 2

Input: typed = "tab", saved = "tables"
Output: false

The lengths differ by 3, so at least 3 inserts are needed.

Constraints

  • 0 <= typed.length, saved.length <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).