Sponsored
Sponsored
This 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.
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.
1#include <iostream>
2#include <vector>
3#include <unordered_set>
4using namespace std;
5
6class TrieNode {
7public:
8 TrieNode* children[26];
9 bool isEndOfWord;
10 TrieNode() {
11 for (int i = 0; i < 26; i++)
12 children[i] = nullptr;
13 isEndOfWord = false;
14 }
15};
16
17class Solution {
18public:
19 void insert(TrieNode* root, const string& word) {
20 TrieNode* node = root;
21 for (char c : word) {
22 int index = c - 'a';
23 if (!node->children[index])
24 node->children[index] = new TrieNode();
25 node = node->children[index];
26 }
27 node->isEndOfWord = true;
28 }
29
30 bool dfs(TrieNode* root, const string& word, int start, int count) {
31 if (start == word.size())
32 return count >= 2;
33
34 TrieNode* node = root;
35 for (int i = start; i < word.size(); i++) {
36 int index = word[i] - 'a';
37 if (!node->children[index])
38 return false;
39 node = node->children[index];
40 if (node->isEndOfWord && dfs(root, word, i + 1, count + 1))
41 return true;
42 }
43
44 return false;
45 }
46
47 vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
48 TrieNode* root = new TrieNode();
49 for (auto& word : words)
50 if (!word.empty())
51 insert(root, word);
52
53 vector<string> result;
54 for (auto& word : words)
55 if (dfs(root, word, 0, 0))
56 result.push_back(word);
57 return result;
58 }
59};
60
61// Usage example
62int main() {
63 Solution sol;
64 vector<string> words = {"cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"};
65 vector<string> concatenatedWords = sol.findAllConcatenatedWordsInADict(words);
66
67 cout << "Concatenated Words:" << endl;
68 for (const string& word : concatenatedWords)
69 cout << word << endl;
70
71 return 0;
72}
73
The C++ implementation uses a Trie data structure to store the words and a DFS mechanism to check if a word can be constructed by combining shorter words within the Trie. During the DFS, every time a complete sub-word is found, it attempts to continue from there, ensuring at least two words are used to generate the concatenated word.
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.
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.
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.