Skip to the code
cf.
+25 XP

#534 Shortest Merged Draft

medium

Two editors wrote short drafts, a and b, and you want one merged string that contains both drafts as subsequences: deleting some characters from the merged string gives a, and deleting some (possibly different) characters gives b.

Write shortestMergeLength(a, b) that returns the length of the shortest such merged string.

Example 1

Input: a = "dark", b = "dart"
Output: 5

darkt contains both. The shared dar is written once.

Example 2

Input: a = "ab", b = "ab"
Output: 2

The drafts are equal, so the merge is just ab.

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).