Skip to content

Commit 686d9b3

Browse files
authored
Create count-items-matching-a-rule.cpp
1 parent 82d4496 commit 686d9b3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

C++/count-items-matching-a-rule.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
7+
static const unordered_map<string, int> rule = {{"type", 0}, {"color", 1}, {"name", 2}};
8+
int result = 0;
9+
for (const auto& item : items) {
10+
if (item[rule.at(ruleKey)] == ruleValue) {
11+
++result;
12+
}
13+
}
14+
return result;
15+
}
16+
};

0 commit comments

Comments
 (0)