-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution208.cpp
More file actions
49 lines (45 loc) · 1.04 KB
/
Solution208.cpp
File metadata and controls
49 lines (45 loc) · 1.04 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
//
// Solution208.cpp
// Algorithm
//
// Created by Pancf on 2020/12/19.
//
#include "Solution208.hpp"
void Trie::insert(std::string word)
{
TrieNode *cur = root;
for (auto ch : word) {
if (cur && cur->links[ch - 'a']) {
cur = cur->links[ch - 'a'];
} else {
TrieNode *node = new TrieNode;
cur->links[ch - 'a'] = node;
cur = node;
}
}
cur->isEnd = true;
}
bool Trie::search(std::string word)
{
TrieNode *cur = root;
int i = 0;
for (; i < word.length(); ++i) {
char ch = word[i];
if (cur && cur->links[ch - 'a']) {
cur = cur->links[ch - 'a'];
} else break;
}
return (i == word.size()) && (cur->isEnd || !cur);
}
bool Trie::startsWith(std::string prefix)
{
TrieNode *cur = root;
int i = 0;
for (; i < prefix.length(); ++i) {
char ch = prefix[i];
if (cur && cur->links[ch - 'a']) {
cur = cur->links[ch - 'a'];
} else break;
}
return (i == prefix.size());
}