#include<string>
#include<iostream>
#include<unordered_map>
using namespace std;
class Trie {
public:
/** Initialize your data structure here. */
Trie() {
result = new Node();
}
class Node
{
public:
Node() { isWord = false; }
bool isWord;
unordered_map<char, Node*> map;
};
/** Inserts a word into the trie. */
void insert(string word) {
Node* temp = result;
for (int i = 0; i < word.size(); ++i) {
if (temp->map.find(word[i]) == temp->map.end()) {
temp->map[word[i]] = new Node();
}
temp = temp->map[word[i]];
}
temp->isWord = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
Node* temp = result;
for (int i = 0; i < word.size(); i++) {
if (temp->map.find(word[i]) == temp->map.end())
return false;
temp = temp->map[word[i]];
}
return temp->isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
Node* temp = result;
for (int i = 0; i < prefix.size(); i++) {
if (temp->map.find(prefix[i]) == temp->map.end())
return false;
temp = temp->map[prefix[i]];
}
return true;
}
private:
Node* result;
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/