-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
95 lines (88 loc) · 2.4 KB
/
Solution.cs
File metadata and controls
95 lines (88 loc) · 2.4 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public class WordDictionary
{
WordDictionary[] children;
bool isEndOfWord;
public WordDictionary()
{
children = new WordDictionary[26];
isEndOfWord = false;
}
public void AddWord(string word)
{
WordDictionary node = this;
foreach (char c in word)
{
int index = c - 'a';
if (node.children[index] == null)
{
node.children[index] = new WordDictionary();
}
node = node.children[index];
}
node.isEndOfWord = true;
}
public bool Search(string word)
{
WordDictionary node = this;
for (int i = 0; i < word.Length; i++)
{
char c = word[i];
if (c == '.')
{
for (int j = 0; j < 26; j++)
{
WordDictionary next = node.children[j];
if (next != null)
{
if (next.Search(word[(i + 1)..]))
{
return true;
}
}
}
return false;
}
int index = c - 'a';
if (node.children[index] == null)
{
return false;
}
node = node.children[index];
}
return node.isEndOfWord;
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.AddWord(word);
* bool param_2 = obj.Search(word);
*/
public class Solution
{
public List<dynamic> Execute(string[] actions, string[][] values)
{
List<dynamic> result = [];
WordDictionary wordDictionary = null;
for (int i = 0; i < actions.Length; i++)
{
switch (actions[i])
{
case "WordDictionary":
wordDictionary = new WordDictionary();
result.Add(null);
break;
case "addWord":
wordDictionary.AddWord(values[i][0]);
result.Add(null);
break;
case "search":
result.Add(wordDictionary.Search(values[i][0]));
break;
default:
break;
}
}
return result;
}
}