Given an array of strings words (without duplicates), return all the 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 (not necessarily distinct) in the given array.
Example 1:
Input: words = ["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".
Example 2:
Input: words = ["cat","dog","catdog"] Output: ["catdog"]
Constraints:
1 <= words.length <= 1041 <= words[i].length <= 30words[i] consists of only lowercase English letters.words are unique.1 <= sum(words[i].length) <= 105This approach involves using a Trie data structure to store all words. We then perform a DFS to check if a word can be formed using other words in the trie. The main advantage of using a Trie is that it provides an efficient way to store and look up words simply by traversing nodes based on each character of the word.
We will mark nodes in the Trie that represent the end of a word and during DFS checks if the current segment of the word is in the Trie.
In the C solution, we create a Trie with nodes for each letter and a boolean flag indicating the end of a word. We add all the words into the Trie and then use a depth-first search (DFS) function to check if a given word can be constructed by concatenating other words in the Trie. If it can be constructed, it is added to the results list.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n * m^2), where n is the number of words and m is the average length of the words. The DFS function can make at most m calls for each word.
Space Complexity: O(n * m) for storing the Trie, where n is the number of words and m is the average length of the words.
In this approach, dynamic programming (DP) is used to efficiently solve the problem of finding concatenated words. The idea is to treat each word as a state and store the results of subproblems to avoid repeated calculations. We iterate over each word and for each segment of the word, check if it can be split into valid sub-words based on previously computed results in the DP array.
This can be visualized as using a DP boolean array where each index represents if the word up to that index can be segmented into valid words in the list. This method is beneficial in reducing the number of redundant calculations.
The C solution uses a hashed linked list structure to store words and checks if each word is a concatenated word using dynamic programming. A boolean array is used to record which parts of the word can be formed by other words. The solution attempts to hash words into buckets and checks for matches to segment words.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n * m^2), where n is the number of words and m is the average length of the words. The space used for the dp array and hash table is the main contributor.
Space Complexity: O(n * m) due to hash table entry needs.
| Approach | Complexity |
|---|---|
| Trie and Depth-First Search (DFS) | Time Complexity: O(n * m^2), where n is the number of words and m is the average length of the words. The DFS function can make at most m calls for each word. Space Complexity: O(n * m) for storing the Trie, where n is the number of words and m is the average length of the words. |
| Dynamic Programming | Time Complexity: O(n * m^2), where n is the number of words and m is the average length of the words. The space used for the dp array and hash table is the main contributor. Space Complexity: O(n * m) due to hash table entry needs. |
Substring with Concatenation of All Words | Leetcode #30 • Techdose • 22,728 views views
Watch 9 more video solutions →Practice Concatenated Words with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor