https://leetcode.com/problems/map-sum-pairs/description/
class MapSum {
public:
map<string, int> pairs;
/** Initialize your data structure here. */
bool starts_with(string s, string t)
{
return s.substr(0, t.size())==t;
}
MapSum() {
}
void insert(string key, int val) {
pairs[key] = val;
}
int sum(string prefix) {
int res = 0;
for(auto p:pairs){
if(starts_with(p.first, prefix)){
res += p.second;
}
}
return res;
}
};