You are viewing a single comment's thread. Return to all comments →
My solution in C++
struct Node { unordered_map<char, Node*> children; int count; Node() { count = 0; } }; class Trie { private: Node *_root; public: Trie() { _root = new Node(); } void add(string str) { Node *node = _root; for (char c : str) { if (node->children.find(c) == node->children.end()) { Node *newNode = new Node(); node->children[c] = newNode; node = newNode; } else { node = node->children[c]; } node->count++; } } int find(string str) { Node *node = _root; for (char c : str) { if (node->children.find(c) == node->children.end()) { return 0; } else { node = node->children[c]; } } return node->count; } };
Seems like cookies are disabled on this browser, please enable them to open this website
Tries: Contacts
You are viewing a single comment's thread. Return to all comments →
My solution in C++