Find the Shortest Superstring

Given an array of strings words, return the smallest string that contains each string in words as a substring. If there are multiple valid strings of the smallest length, return any of them.

You may assume that no string in words is a substring of another string in words.

Example 1
Inputwords = ["alex","loves","leetcode"]
Output"alexlovesleetcode"
All permutations of "alex", "loves", and "leetcode" would also be accepted.
Example 2
Inputwords = ["catg","ctaagt","gcta","ttca","atgcatc"]
Output"gctaagttcatgcatc"
The returned string contains every word in words as a substring and has the smallest possible length.

Constraints

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

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate