Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Trie {
- public:
- Trie() {}
- void insert(string word) {
- Trie* node = this;
- for (char ch : word) {
- if (node->next.find(ch)==node->next.end()) { node->next[ch] = new Trie(); }
- node = node->next[ch];
- }
- node->isword = true;
- }
- bool search(string word) {
- Trie* node = this;
- for (char ch : word) {
- if (node->next.find(ch)==node->next.end()) { return false; }
- node = node->next[ch];
- }
- return node->isword;
- }
- bool startsWith(string prefix) {
- Trie* node = this;
- for (char ch : prefix) {
- if (node->next.find(ch)==node->next.end()) { return false; }
- node = node->next[ch];
- }
- return true;
- }
- private:
- map<char, Trie*> next = {};
- bool isword = false;
- };
Add Comment
Please, Sign In to add comment