题解 | #字典树的实现#
字典树的实现
http://www.nowcoder.com/practice/a55a584bc0ca4a83a272680174be113b
class Solution {
public:
/**
*
* @param operators string字符串vector<vector<>> the ops
* @return string字符串vector
*/
unordered_map<string, int> mp,pre;
vector<string> trieU(vector<vector<string> >& operators) {
// write code here
vector<string> ans;
for(int i = 0;i < operators.size();i++){
string op = operators[i][0],str = operators[i][1];
if(op == "1"){
mp[str]++;
for(int i = 1;i <= str.size();i++){
pre[str.substr(0,i)]++;
}
}
if(op == "2"){
mp[str]--;
for(int i = 1;i <= str.size();i++){
pre[str.substr(0,i)]--;
}
}
if(op == "3"){
if(mp[str]) ans.push_back("YES");
else ans.push_back("NO");
}
if(op == "4"){
ans.push_back(to_string(pre[str]));
}
}
return ans;
}
};