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.
1class TrieNode {
2 constructor() {
3 this.children = {};
4 this.isEndOfWord = false;
5 }
6}
7
8class Solution {
9 insert(root, word) {
10 let node = root;
11 for (let char of word) {
12 if (!node.children[char]) {
13 node.children[char] = new TrieNode();
14 }
15 node = node.children[char];
16 }
17 node.isEndOfWord = true;
18 }
19
20 dfs(root, word, start, count) {
21 if (start === word.length) {
22 return count >= 2;
23 }
24
25 let node = root;
26 for (let i = start; i < word.length; i++) {
27 let char = word[i];
28 if (!node.children[char]) {
29 return false;
30 }
31 node = node.children[char];
32 if (node.isEndOfWord && this.dfs(root, word, i + 1, count + 1)) {
33 return true;
34 }
35 }
36
37 return false;
38 }
39
40 findAllConcatenatedWordsInADict(words) {
41 const root = new TrieNode();
42 for (let word of words) {
43 if (word.length > 0) {
44 this.insert(root, word);
45 }
46 }
47
48 const result = [];
49 for (let word of words) {
50 if (this.dfs(root, word, 0, 0)) {
51 result.push(word);
52 }
53 }
54 return result;
55 }
56}
57
58// Usage example
59const solution = new Solution();
60const words = ["cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"];
61const concatenatedWords = solution.findAllConcatenatedWordsInADict(words);
62console.log("Concatenated Words:");
63concatenatedWords.forEach(word => console.log(word));
64
The JavaScript solution initializes a Trie and recursively checks if words can be split into multiple concatenated parts using DFS. The approach mirrors other language solutions by using a Trie to manage and search for word fragments.
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.