Find the Shortest Superstring

Hard

Description

Given an array of unique strings words, return the shortest string that contains each string in words as a substring.

Examples

Input:words = ["alex","loves","leetcode"]
Output:"alexlovesleetcode"
Explanation:

Shortest superstring.

Input:words = ["cat","dog","tcat","tcatdog"]
Output:tcatdog
Explanation:

"tcatdog" contains all other words as substrings, making it the shortest superstring.

Input:words = ["ab","bc","cd"]
Output:abcd
Explanation:

Overlapping "ab" with "bc" (sharing 'b') and "bc" with "cd" (sharing 'c') creates the superstring "abcd".

Constraints

  • 1 ≤ words.length ≤ 12

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!