LeetCode Problem

How to Solve Find the Shortest Superstring

Start by calculating pairwise overlaps between words to determine the optimal concatenation order. Use dynamic programming with a bitmask representing used words to efficiently track and build the shortest superstring. Reconstruct the final string by backtracking through the DP table to assemble the sequence that achieves maximum overlap while covering all words exactly once.

GhostInterview Help

Need help with Find the Shortest Superstring without spending extra time grinding it?

GhostInterview can read Find the Shortest Superstring from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #943State transition dynamic programmingReviewed 2026-03-07
Difficulty
Hard
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

Start by calculating pairwise overlaps between words to determine the optimal concatenation order. Use dynamic programming with a bitmask representing used words to efficiently track and build the shortest superstring. Reconstruct the final string by backtracking through the DP table to assemble the sequence that achieves maximum overlap while covering all words exactly once.

Problem Statement

Given an array of unique strings, your task is to return the shortest string that contains each word as a substring. If multiple strings have the same minimal length, any valid one is acceptable. No word in the array is a substring of another.

For example, given words = ["alex","loves","leetcode"], one shortest superstring is "alexlovesleetcode". The problem requires considering all permutations and overlaps, emphasizing efficient state transition dynamic programming rather than brute force concatenation.

Examples

Example 1

Input: words = ["alex","loves","leetcode"]

Output: "alexlovesleetcode"

All permutations of "alex","loves","leetcode" would also be accepted.

Example 2

Input: words = ["catg","ctaagt","gcta","ttca","atgcatc"]

Output: "gctaagttcatgcatc"

Example details omitted.

Constraints

  • 1 <= words.length <= 12
  • 1 <= words[i].length <= 20
  • words[i] consists of lowercase English letters.
  • All the strings of words are unique.

Solution Approach

Compute Pairwise Overlaps

Create a matrix storing the maximum overlap length between each pair of words. This allows the DP step to quickly know how much each word can extend another, reducing redundant computation and directly tying into the state transition dynamic programming pattern.

Dynamic Programming with Bitmask

Use a DP table where dp[mask][i] represents the shortest superstring ending with word i using words indicated by mask. Transition by adding a word j not in mask and update dp[mask | (1<<j)][j] using previously computed overlaps. This method handles all subsets efficiently while controlling exponential growth.

Reconstruct Shortest Superstring

After filling the DP table, backtrack from the state with all words used to reconstruct the order of words that yields the shortest superstring. Concatenate the words respecting the precomputed overlaps to produce the final minimal-length result.

Complexity Analysis

MetricValue
TimeO(N^2 (2^N + W))
SpaceO(N (2^N + W))

Time complexity is O(N^2 * 2^N + N^2 * W) due to computing overlaps and DP transitions, where N is the number of words and W is the average word length. Space complexity is O(N * 2^N + N^2) to store DP states and overlaps.

What Interviewers Usually Probe

  • Expect discussion of bitmask DP and its exponential subset growth pattern.
  • Look for correct calculation of word overlaps and handling edge cases in reconstruction.
  • Watch for clarity in state transition definition and why each DP entry correctly represents a partial solution.

Common Pitfalls or Variants

Common pitfalls

  • Ignoring the fact that overlaps are directional and dp[i][mask] must track the ending word.
  • Attempting brute-force permutation generation which is infeasible for N > 10.
  • Forgetting to reconstruct the string correctly after DP, leading to invalid superstrings.

Follow-up variants

  • Minimize superstring for a subset of words where some words may repeat.
  • Find a superstring using weighted overlaps for prioritizing certain word concatenations.
  • Compute the shortest superstring but allow some words to be omitted based on constraints.

How GhostInterview Helps

  • GhostInterview suggests the optimal bitmask DP formulation for tracking subsets of words.
  • It guides on calculating pairwise overlaps efficiently to feed into the DP transitions.
  • The tool demonstrates backtracking to reconstruct the correct minimal superstring, reducing common reconstruction errors.

Topic Pages

FAQ

What is the main pattern used in Find the Shortest Superstring?

The problem is solved using state transition dynamic programming with bitmasking to track subsets of words efficiently.

Can the words in the input array be substrings of each other?

No, the problem guarantees that no word is a substring of another, simplifying overlap calculations.

Why is a brute-force permutation approach impractical?

Because the number of permutations grows factorially with the number of words, leading to infeasible computation for N > 10.

How are overlaps computed between two words?

For each pair, calculate the longest suffix of the first matching a prefix of the second, which determines the maximal overlap length.

Does GhostInterview provide step-by-step reconstruction guidance?

Yes, it walks through the backtracking of the DP table to assemble the shortest superstring correctly.

GhostInterview Solver

Need direct help with Find the Shortest Superstring instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Find the Shortest Superstring from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.