1773. 统计匹配检索规则的物品数量
思路
可以利用哈希表把输入 \textit{ruleKey}ruleKey 转换为 \textit{items}[i]items[i] 的下标,然后再遍历一遍 \textit{items}items,找出符合条件的物品数量。
class Solution {
public:
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
int idx = 0;
if (ruleKey == "color") {
idx = 1;
} else if (ruleKey == "name") {
idx = 2;
}
int cnt = 0;
for (auto strs : items) {
if (strs[idx] == ruleValue) {
cnt++;
}
}
return cnt;
}
};