You are viewing a single comment's thread. Return to all comments →
I did the same. Here is my C++ solution. I probably could have come up with a better class name. What to name thing is always the challenge, isn't it?
class Words { public: Words() {} int fullCountUnder; bool isFullWord; map<char, Words*> m; void insert(string text) { Words* word = this; word->fullCountUnder++; for (char &c : text) { Words* prev = word; word = word->has(c); if (word == NULL) { Words * newWord = new Words(); prev->m.insert(pair<char, Words*>(c, newWord)); word = newWord; } word->fullCountUnder++; } if (word != this) { word->isFullWord = true; } } Words* has(char c) { map<char, Words*>::iterator it = this->m.find(c); if (it != this->m.end()) { return it->second; } return NULL; } int matchCount(string partial) { Words* word = this; for (char& c : partial) { word = word->has(c); if (word == NULL) { return 0; } } return word->fullCountUnder; } };
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 →
I did the same. Here is my C++ solution. I probably could have come up with a better class name. What to name thing is always the challenge, isn't it?