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.
1import java.util.*;
2
3class TrieNode {
4 TrieNode[] children = new TrieNode[26];
5 boolean isEndOfWord = false;
6}
7
8public class Solution {
9 public void insert(TrieNode root, String word) {
10 TrieNode node = root;
11 for (char ch : word.toCharArray()) {
12 int index = ch - 'a';
13 if (node.children[index] == null) {
14 node.children[index] = new TrieNode();
15 }
16 node = node.children[index];
17 }
18 node.isEndOfWord = true;
19 }
20
21 public boolean dfs(TrieNode root, String word, int start, int count) {
22 if (start == word.length()) {
23 return count >= 2;
24 }
25
26 TrieNode node = root;
27 for (int i = start; i < word.length(); i++) {
28 int index = word.charAt(i) - 'a';
29 if (node.children[index] == null) {
30 return false;
31 }
32 node = node.children[index];
33 if (node.isEndOfWord && dfs(root, word, i + 1, count + 1)) {
34 return true;
35 }
36 }
37
38 return false;
39 }
40
41 public List<String> findAllConcatenatedWordsInADict(String[] words) {
42 TrieNode root = new TrieNode();
43 for (String word : words) {
44 if (!word.isEmpty()) {
45 insert(root, word);
46 }
47 }
48
49 List<String> result = new ArrayList<>();
50 for (String word : words) {
51 if (dfs(root, word, 0, 0)) {
52 result.add(word);
53 }
54 }
55 return result;
56 }
57
58 public static void main(String[] args) {
59 Solution sol = new Solution();
60 String[] words = {"cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"};
61 List<String> concatenatedWords = sol.findAllConcatenatedWordsInADict(words);
62 System.out.println("Concatenated Words:");
63 concatenatedWords.forEach(System.out::println);
64 }
65}
66
The Java solution also uses a Trie to efficiently check if words can be constructed by concatenating other words in the list. The approach creates each word in the Trie and uses a DFS to determine if the word can be segmented into smaller words that are also within the Trie. If successful with two or more segments, it's considered a 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 Java solution employs a HashSet and a dynamic programming table. The DP array indicates whether every index in the string can be built from prefixes found in the dictionary. Each valid concatenation check updates the last element of the DP array.