-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
83 lines (75 loc) · 2.08 KB
/
Solution.cs
File metadata and controls
83 lines (75 loc) · 2.08 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
public class StreamChecker
{
Trie root = new Trie();
List<char> list = new();
public StreamChecker(string[] words)
{
foreach (string word in words)
{
Trie cur = root;
foreach (char ch in word.Reverse())
{
int index = ch - 'a';
if (cur.Children[index] == null)
{
cur.Children[index] = new();
}
cur = cur.Children[index];
}
cur.IsWord = true;
}
}
public bool Query(char letter)
{
list.Add(letter);
Trie cur = root;
for (int i = list.Count - 1; i >= 0; i--)
{
int index = list[i] - 'a';
if (cur.IsWord) return true;
if (cur.Children[index] == null)
{
return false;
}
cur = cur.Children[index];
}
return cur.IsWord;
}
}
public class Trie
{
public Trie[] Children = new Trie[26];
public bool IsWord = false;
}
/**
* Your StreamChecker object will be instantiated and called as such:
* StreamChecker obj = new StreamChecker(words);
* bool param_1 = obj.Query(letter);
*/
public class Solution
{
public List<dynamic> Execute(string[] events, dynamic values)
{
List<dynamic> result = [];
StreamChecker streamChecker = null;
object[] objectList = JsonConvert.DeserializeObject<object[]>(values);
for (int i = 0; i < events.Length; i++)
{
switch (events[i])
{
case "StreamChecker":
streamChecker = new StreamChecker(CastType<string[]>(objectList[i]));
result.Add(null);
break;
case "query":
result.Add(streamChecker.Query(CastType<char>(objectList[i])));
break;
}
}
return result;
}
private T CastType<T>(object value)
{
return JsonConvert.DeserializeObject<T[]>(JsonConvert.SerializeObject(value))[0];
}
}