Concatenated Words
Given a list of distinct lowercase words, return every word that can be formed by concatenating at least two shorter words taken from the same list.
Rules that follow from the definition:
- A word may not be a component of itself. The whole word must never be the only piece.
- A shorter word may be reused as often as needed inside one assembly.
- A component has to be strictly shorter than the word being built.
- Return the matching words in the order they appear in the input list.
- Return an empty list when no word matches.
Examples
Input: ["ab","cd"]
Output: []
Input: ["a"]
Output: []
Hints
Build a Trie with all words, then check each word if it can be formed from at least two shorter words.
Use DP to check if a word can be formed by concatenating other words from the list.
Processing words shortest first makes shorter components available before they are needed; that ordering is an internal convenience only, so return the matches in the original input order.
Related Problems
Concatenated Words
Given a list of distinct lowercase words, return every word that can be formed by concatenating at least two shorter words taken from the same list.