Map Sum Pairs LeetCode solution in C++

1
class MapSum {
2
public:
3
map<string, int> mp;
4
MapSum() {}
5

6
void insert(string key, int val) {
7
mp[key] = val;
8
}
9

10
int sum(string prefix) {
11
int count = 0;
12
for (auto i : mp) {
13
int j;
14
for (j = 0; j < i.first.size() && j < prefix.size(); j++) {
15
if (i.first[j] != prefix[j]) break;
16
}
17
if (j == prefix.size()) count += i.second;
18
}
19
return count;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0