-
Notifications
You must be signed in to change notification settings - Fork 112
/
0211-AddAndSearchWordDataStructureDesign.cs
78 lines (68 loc) · 2.32 KB
/
0211-AddAndSearchWordDataStructureDesign.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
72
73
74
75
76
77
78
//-----------------------------------------------------------------------------
// Runtime: 272ms
// Memory Usage: 45 MB
// Link: https://leetcode.com/submissions/detail/366155243/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0211_AddAndSearchWordDataStructureDesign
{
private readonly Trie root;
/** Initialize your data structure here. */
public _0211_AddAndSearchWordDataStructureDesign()
{
root = new Trie();
}
/** Adds a word into the data structure. */
public void AddWord(string word)
{
var current = root;
foreach (var ch in word)
{
if (!current.Children.ContainsKey(ch))
current.Children.Add(ch, new Trie());
current = current.Children[ch];
}
current.Finished = true;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public bool Search(string word)
{
return Search(word, root);
}
private bool Search(string word, Trie startPoint)
{
var current = startPoint;
for (int i = 0; i < word.Length; i++)
{
var ch = word[i];
if (ch == '.')
{
foreach (var key in current.Children.Keys)
{
if (Search(word.Substring(i + 1), current.Children[key]))
return true;
}
return false;
}
else if (current.Children.ContainsKey(ch))
current = current.Children[ch];
else
return false;
}
return current.Finished;
}
private class Trie
{
public Dictionary<char, Trie> Children = new Dictionary<char, Trie>();
public bool Finished = false;
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.AddWord(word);
* bool param_2 = obj.Search(word);
*/
}