item [i] = [typei、colori、namei]で、i番目のアイテムのタイプ、色、名前を説明するアイテムの配列を提供します。
また、ruleKeyとruleValueの2つの文字列で表される取得ルールを提供します。
i番目の記事が次の条件のいずれかを満たすことができる場合、指定された検索ルールに一致すると見なされます。
ruleKey == "type"およびruleValue == typei。
ruleKey == "color"およびruleValue == colori。
ruleKey == "name"およびruleValue == namei。
検索ルールに一致するアイテムの数をカウントして返します。
ruleKeyから添え字へのマッピングを作成してから、トラバースします。
class Solution {
public:
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
unordered_map<string, int> keyToIndex = {
{
"type", 0}, {
"color", 1}, {
"name", 2}};
int count = 0;
for (vector<string> &svec : items) {
if (svec[keyToIndex[ruleKey]] == ruleValue) {
++count;
}
}
return count;
}
};