Concatenated Words - Complete Solution Guide
Concatenated Words is LeetCode problem 472, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 "c
Detailed Explanation
The problem asks us to identify concatenated words from a given list of strings. A concatenated word is a word that can be formed by combining at least two shorter words from the same list. The words in the list are unique, and the concatenated words may be formed by using the same word multiple times.
Solution Approach
The solution combines sorting, a Trie data structure, and dynamic programming. First, the words are sorted by their length in ascending order. This ensures that when checking for concatenated words, all possible shorter words that could make up the concatenated word are already present in the Trie. A Trie is used to store the words seen so far. For each word, a dynamic programming approach determines if it can be constructed from shorter words present in the Trie. If it can, it's added to the result list. The word is then added to the Trie so other longer words can reuse it.
Step-by-Step Algorithm
- Step 1: Sort the input array of strings `words` by length in ascending order.
- Step 2: Initialize an empty Trie data structure to store the words.
- Step 3: Initialize an empty list `result` to store the concatenated words.
- Step 4: Iterate through each word in the sorted `words` array.
- Step 5: For each word, use dynamic programming to check if it can be formed by concatenating shorter words already present in the Trie.
- Step 6: If the dynamic programming check returns true (i.e., the word is a concatenated word), add the word to the `result` list.
- Step 7: Add the current word to the Trie.
- Step 8: After iterating through all words, return the `result` list.
Key Insights
- Insight 1: Sorting the words by length helps to efficiently build up the Trie and search for concatenated words. Shorter words can be added to the Trie first, ensuring that when a longer word is checked, all possible component words are already in the Trie.
- Insight 2: Using Dynamic Programming (DP) to check if a word is concatenated is crucial for efficiency. The DP approach avoids redundant checks by storing intermediate results, indicating whether a prefix of the word can be formed by concatenating shorter words.
- Insight 3: Trie data structure is used to efficiently determine if a substring of a word is present in the list of words.
- Insight 4: A word cannot be constructed by itself according to the definition in the question. Thus, when finding smaller words in Trie, a concatenated word must contains at least two smaller words.
Complexity Analysis
Time Complexity: O(N * L^3)
Space Complexity: O(N * L)
Topics
This problem involves: Array, String, Dynamic Programming, Depth-First Search, Trie, Sorting.
Companies
Asked at: eBay.