472 Concatenated Words
472. Concatenated Words
1. Question
Given a list of words (without duplicates), please write a program that returns all concatenated words in the given 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.
Example:
Input: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]
Explanation:
"catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".Note:
The number of elements of the given array will not exceed
10,000The length sum of elements in the given array will not exceed
600,000.All the input string will only include lower case letters.
The returned elements order does not matter.
2. Implementation
(1) Trie + DFS
3. Time & Space Complexity
Trie + DFS:时间复杂度O(n * L^2), n为word的个数,l为每个word的平均长度, 空间复杂度O(nL)
Last updated
Was this helpful?