 All Problems
Concatenated Words
hard
trie
dynamic programming
depth-first search
string
google
amazon
facebook

Given an array of strings words (without duplicates), return all the concatenated words in the provided list of words.

A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array of words.

Example 1:

Input:
cat cats catsdogcats dog dogcatsdog hippopotamuses rat ratcatdogcat
Output:
catsdogcats
dogcatsdog
ratcatdogcat

Example 2:

Input:
a
Output:
(empty)

Constraints:

  • 1 ≤ words.length ≤ 10⁴
  • 1 ≤ words[i].length ≤ 30

Input format: Space-separated words on one line.

Output format: Concatenated words, one per line (in order found).

Run to check your code against the sample cases, or submit to run every case