Skip to the code
cf.
+25 XP

#532 Cheapest Deletions by Character Code

medium

A text-sync tool makes two strings equal by deleting characters from either one. Deleting a character costs its character code, char.charCodeAt(0), so deleting "a" costs 97.

Write cheapestDeletions(a, b) that returns the smallest total cost of deletions that make a and b equal.

Example 1

Input: a = "ab", b = "ba"
Output: 194

Delete a from both to keep b: 97 + 97 = 194. Keeping a instead costs 98 + 98.

Example 2

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

They already match.

Constraints

  • 1 <= a.length, b.length <= 1000
  • Both contain only lowercase English letters.
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).