← DiffPush

Shortest Common Supersequence

DiffPush Tier Dynamic Programming · DP on Strings O(n*m) · O(n*m)

The Broadcast Schedule Merger

A radio network must interleave two station playlists into one master broadcast so that every song of each playlist still airs in its original relative order. Ad space is expensive, so the merged reel must be as short as possible. Several optimal reels may exist; any shortest merge is acceptable.

Input: Two strings str1 and str2.

Output: Return any shortest merged string that embeds both inputs as in-order character sequences.

Constraints

Examples

Example 1

Input: {"str1":"abac","str2":"cab"}
Output: "cabac"
"cabac" contains "abac" (positions 2-5) and "cab" (positions 1-3); no shorter merge exists.

Example 2

Input: {"str1":"abc","str2":"abc"}
Output: "abc"
Identical strings merge into themselves.

Solve this in your browser →

Also on LeetCode ↗