2-D Dynamic Programming Bitmask DP
601. Find the Shortest Superstring
You are given a list of distinct lowercase words, words. Return the shortest string that contains every
one of them.
A word is contained in a string when its letters appear there one after another, with nothing between
them. Two words can share letters: when the end of one word is the start of the next, those letters are
written once. For example, "tenorbit" contains both "tenor" and "orbit", which share or.
Often several shortest strings exist, and any of them is correct. When the string you return contains every
word, the result shows its length, as a superstring of length 15. Otherwise it shows the first word it is
missing, such as words[2] = "tenor" is not in the answer. Positions count from 0.
Example 1
Input: words = ["bitter", "terrace", "tenor", "orbit"]
"tenorbitterrace"Explanation: In the order tenor, orbit, bitter, terrace, each word shares letters with the next: or, then
bit, then ter. That saves 8 of the 23 letters, and no order saves more.
Example 2
Input: words = ["kiwi", "plum", "fig"]
"figplumkiwi"Explanation: No word ends with letters that another word starts with, so nothing can be shared. All three words written one after another, in any order, is correct.
Example 3
Input: words = ["dad", "add"]
"dadd"Explanation: With dad first, the two words share ad, which gives 4 letters. With add first, they share only d,
and addad has 5.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ words.length ≤ 161 ≤ words[i].length ≤ 20words[i]consists of lowercase English letters.- The words are distinct, and no word is contained in another.
Clarifying questions
What you would ask an interviewer before writing any code.