-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1032.cs
74 lines (62 loc) · 2.08 KB
/
Solution1032.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
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1032
{
/// <summary>
/// 1032. Stream of Characters - Hard
/// <a href="https://leetcode.com/problems/stream-of-characters</a>
/// </summary>
public class StreamChecker
{
private class TrieNode
{
public Dictionary<char, TrieNode> Children = [];
public bool IsEnd = false;
}
private TrieNode root;
private Queue<char> stream;
private int maxWordLength;
public StreamChecker(string[] words)
{
root = new TrieNode();
stream = new Queue<char>();
maxWordLength = 0;
// Insert words in reverse order into Trie
foreach (var word in words)
{
InsertWord(word);
maxWordLength = Math.Max(maxWordLength, word.Length);
}
}
private void InsertWord(string word)
{
TrieNode node = root;
for (int i = word.Length - 1; i >= 0; i--) // Reverse insert
{
char c = word[i];
if (!node.Children.ContainsKey(c))
node.Children[c] = new TrieNode();
node = node.Children[c];
}
node.IsEnd = true; // Mark end of word
}
public bool Query(char letter)
{
stream.Enqueue(letter);
if (stream.Count > maxWordLength)
stream.Dequeue(); // Maintain only last maxWordLength chars
TrieNode node = root;
var streamArray = stream.ToArray();
// Traverse the Trie using the recent characters (from latest to oldest)
for (int i = streamArray.Length - 1; i >= 0; i--)
{
char c = streamArray[i];
if (!node.Children.ContainsKey(c)) return false; // No match
node = node.Children[c];
if (node.IsEnd) return true; // Found a valid word
}
return false;
}
}
}