Skip to the code
+25 XP

#539 Weighted Edit Cost

medium

A collaborative editor charges different costs for different kinds of change when it measures how much a document title was edited. Inserting a character costs insertCost, deleting one costs deleteCost, and replacing one character with another costs replaceCost.

Write weightedEditCost(a, b, insertCost, deleteCost, replaceCost) that returns the smallest total cost to turn a into b.

Example 1

Input: a = "cat", b = "cut", insertCost = 1, deleteCost = 1, replaceCost = 5
Output: 2

Replacing a costs 5, but deleting a (1) and inserting u (1) costs 2.

Example 2

Input: a = "", b = "abc", insertCost = 2, deleteCost = 1, replaceCost = 1
Output: 6

Three inserts at 2 each.

Constraints

  • 0 <= a.length, b.length <= 500
  • 1 <= insertCost, deleteCost, replaceCost <= 100
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

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