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.
1using System;
2using System.Collections.Generic;
3
4public class TrieNode {
5 public TrieNode[] Children = new TrieNode[26];
6 public bool IsEndOfWord;
7}
8
9public class Solution {
10 public void Insert(TrieNode root, string word) {
11 var node = root;
12 foreach (char c in word) {
13 int index = c - 'a';
14 if (node.Children[index] == null)
15 node.Children[index] = new TrieNode();
16 node = node.Children[index];
17 }
18 node.IsEndOfWord = true;
19 }
20
21 public bool Dfs(TrieNode root, string word, int start, int count) {
22 if (start == word.Length)
23 return count >= 2;
24
25 var node = root;
26 for (int i = start; i < word.Length; i++) {
27 int index = word[i] - 'a';
28 if (node.Children[index] == null)
29 return false;
30 node = node.Children[index];
31 if (node.IsEndOfWord && Dfs(root, word, i + 1, count + 1))
32 return true;
33 }
34
35 return false;
36 }
37
38 public List<string> FindAllConcatenatedWordsInADict(string[] words) {
39 TrieNode root = new TrieNode();
40 foreach (var word in words)
41 if (!string.IsNullOrEmpty(word))
42 Insert(root, word);
43
44 List<string> result = new List<string>();
45 foreach (var word in words)
46 if (Dfs(root, word, 0, 0))
47 result.Add(word);
48 return result;
49 }
50
51 public static void Main() {
52 Solution sol = new Solution();
53 string[] words = {"cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"};
54 List<string> concatenatedWords = sol.FindAllConcatenatedWordsInADict(words);
55 Console.WriteLine("Concatenated Words:");
56 foreach (var word in concatenatedWords)
57 Console.WriteLine(word);
58 }
59}
60
The C# implementation builds a TrieNode structure to hold words, and uses recursion in the form of DFS to check if combinations of words are present. This takes advantage of a similar logical structure to other language implementations albeit with C# syntax.
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.
using System.Collections.Generic;
public class Solution {
public List<string> FindAllConcatenatedWordsInADict(string[] words) {
HashSet<string> dict = new HashSet<string>(words);
List<string> result = new List<string>();
foreach (var word in words) {
if (IsConcatenatedWord(word, dict)) {
result.Add(word);
}
}
return result;
}
private bool IsConcatenatedWord(string word, HashSet<string> dict) {
int n = word.Length;
if (n == 0) return false;
bool[] dp = new bool[n + 1];
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && dict.Contains(word.Substring(j, i - j))) {
dp[i] = true;
break;
}
}
}
return dp[n];
}
public static void Main(string[] args) {
Solution sol = new Solution();
string[] words = {"cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"};
List<string> concatenatedWords = sol.FindAllConcatenatedWordsInADict(words);
Console.WriteLine("Concatenated Words:");
foreach (var word in concatenatedWords)
Console.WriteLine(word);
}
}
In the C# solution, the use of HashSet increases lookup speeds when determining potential word concatenations. With a Boolean DP array that records feasible concatenations leading to the last index, it carefully negotiates an efficient path through each character with continued internal validation through pre-processing.