Skip to the code
cf.
+25 XP

#531 Fewest Deletions to Match Two Slugs

medium

Two pages have URL slugs a and b that are supposed to be the same. To find how far apart they are, you may only delete characters, from either slug, one at a time.

Write fewestDeletions(a, b) that returns the fewest deletions needed to make the two slugs equal.

Example 1

Input: a = "card", b = "cart"
Output: 2

Delete d from card and t from cart; both become car.

Example 2

Input: a = "menu", b = "menu"
Output: 0

They already match.

Constraints

  • 1 <= a.length, b.length <= 500
  • Both contain lowercase letters, digits, and -.
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).