Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c8ccdc

Browse files
1032. Stream of Characters
1 parent 9f4a833 commit 5c8ccdc

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎stream-of-characters.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Runtime: 276 ms
2+
// Memory Usage: 99.7 MB
3+
class StreamChecker {
4+
public:
5+
6+
struct Node {
7+
bool word;
8+
vector<Node*> children;
9+
10+
Node() {
11+
word = false;
12+
children = vector<Node*>(26, NULL);
13+
}
14+
};
15+
16+
Node* root = NULL;
17+
string str = "";
18+
StreamChecker(vector<string>& words) {
19+
root = new Node();
20+
21+
for (string s : words) {
22+
Node* crawl = root;
23+
for (int i = s.size() - 1; i >= 0; i--) {
24+
if (!crawl->children[s[i] - 'a']) {
25+
crawl->children[s[i] - 'a'] = new Node();
26+
}
27+
crawl = crawl->children[s[i] - 'a'];
28+
}
29+
crawl->word = true;
30+
}
31+
32+
}
33+
34+
bool query(char letter) {
35+
str += letter;
36+
Node* crawl = root;
37+
for (int i = str.size() - 1; i >= 0; i--) {
38+
if (!crawl->children[str[i] - 'a']) return false;
39+
crawl = crawl->children[str[i] - 'a'];
40+
if (crawl->word) return true;
41+
}
42+
return false;
43+
}
44+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /