0%

1773. Count Items Matching a Rule

hashmap O(n) time O(n) space

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
unordered_map<string, int> types, colors, names;
for (int n = size(items), i = 0; i < n; ++i) {
++types[items[i][0]];
++colors[items[i][1]];
++names[items[i][2]];
}
if (ruleKey == "type") return types[ruleValue];
if (ruleKey == "color") return colors[ruleValue];
return names[ruleValue];
}
};