-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplaceWords.cs
71 lines (62 loc) · 2.22 KB
/
ReplaceWords.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class Solution {
// TrieNode class to represent each node in the Trie
public class TrieNode {
public Dictionary<char, TrieNode> Children { get; set; } = new Dictionary<char, TrieNode>();
public string Word { get; set; }
}
// Trie class to manage the Trie operations
public class Trie {
private readonly TrieNode _root = new TrieNode();
// Method to insert a word into the Trie
public void Insert(string word) {
var node = _root;
foreach (var ch in word) {
if (!node.Children.ContainsKey(ch)) {
node.Children[ch] = new TrieNode();
}
node = node.Children[ch];
}
node.Word = word;
}
// Method to search for the shortest root of a given word in the Trie
public string SearchRoot(string word) {
var node = _root;
foreach (var ch in word) {
if (node.Children.ContainsKey(ch)) {
node = node.Children[ch];
if (node.Word != null) {
return node.Word;
}
} else {
break;
}
}
return null;
}
}
// Method to replace words in the sentence with the shortest root from the dictionary
public string ReplaceWords(IList<string> dictionary, string sentence) {
var trie = new Trie();
// Insert all roots into the Trie
foreach (var root in dictionary) {
trie.Insert(root);
}
var words = sentence.Split(' ');
var result = new StringBuilder();
// Replace each word in the sentence with the shortest root
foreach (var word in words) {
var replacement = trie.SearchRoot(word);
if (replacement != null) {
result.Append(replacement);
} else {
result.Append(word);
}
result.Append(' ');
}
// Remove the trailing space
if (result.Length > 0) {
result.Length--;
}
return result.ToString();
}
}