1773-统计匹配检索规则的物品数量

Raphael Liu Lv10

给你一个数组 items ,其中 items[i] = [typei, colori, namei] ,描述第 i 件物品的类型、颜色以及名称。

另给你一条由两个字符串 ruleKeyruleValue 表示的检索规则。

如果第 i 件物品能满足下述条件之一,则认为该物品与给定的检索规则 匹配

  • ruleKey == "type"ruleValue == typei
  • ruleKey == "color"ruleValue == colori
  • ruleKey == "name"ruleValue == namei

统计并返回 匹配检索规则的物品数量

示例 1:

**输入:** items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
**输出:** 1
**解释:** 只有一件物品匹配检索规则,这件物品是 ["computer","silver","lenovo"] 。

示例 2:

**输入:** items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
**输出:** 2
**解释:** 只有两件物品匹配检索规则,这两件物品分别是 ["phone","blue","pixel"] 和 ["phone","gold","iphone"] 。注意,["computer","silver","phone"] 未匹配检索规则。

提示:

  • 1 <= items.length <= 104
  • 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
  • ruleKey 等于 "type""color""name"
  • 所有字符串仅由小写字母组成

方法一:模拟

思路

可以利用哈希表把输入 ruleKey 转换为 items}[i] 的下标,然后再遍历一遍 items,找出符合条件的物品数量。

代码

[sol1-Python3]
1
2
3
4
class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
index = {"type": 0, "color": 1, "name": 2}[ruleKey]
return sum(item[index] == ruleValue for item in items)
[sol1-Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int index = new HashMap<String, Integer>() { {
put("type", 0);
put("color", 1);
put("name", 2);
} }.get(ruleKey);
int res = 0;
for (List<String> item : items) {
if (item.get(index).equals(ruleValue)) {
res++;
}
}
return res;
}
}
[sol1-C#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Solution {
public int CountMatches(IList<IList<string>> items, string ruleKey, string ruleValue) {
int index = new Dictionary<string, int>() {
{"type", 0}, {"color", 1}, {"name", 2}
}[ruleKey];
int res = 0;
foreach (IList<string> item in items) {
if (item[index].Equals(ruleValue)) {
res++;
}
}
return res;
}
}
[sol1-C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
unordered_map<string, int> dictionary = { {"type", 0}, {"color", 1}, {"name", 2} };
int res = 0, index = dictionary[ruleKey];
for (auto &&item : items) {
if (item[index] == ruleValue) {
res++;
}
}
return res;
}
};
[sol1-C]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int countMatches(char *** items, int itemsSize, int* itemsColSize, char * ruleKey, char * ruleValue) {   
int res = 0, index = 0;
if (strcmp(ruleKey, "type") == 0) {
index = 0;
} else if (strcmp(ruleKey, "color") == 0) {
index = 1;
} else if (strcmp(ruleKey, "name") == 0) {
index = 2;
}
for (int i = 0; i < itemsSize; i++) {
if (strcmp(items[i][index], ruleValue) == 0) {
res++;
}
}
return res;
}
[sol1-JavaScript]
1
2
3
4
5
6
7
8
9
10
var countMatches = function(items, ruleKey, ruleValue) {
const index = {"type":0, "color":1, "name":2}[ruleKey];
let res = 0;
for (const item of items) {
if (item[index] === ruleValue) {
res++;
}
}
return res;
};
[sol1-Golang]
1
2
3
4
5
6
7
8
9
10
11
var d = map[string]int{"type": 0, "color": 1, "name": 2}

func countMatches(items [][]string, ruleKey, ruleValue string) (ans int) {
index := d[ruleKey]
for _, item := range items {
if item[index] == ruleValue {
ans++
}
}
return
}

复杂度分析

  • 时间复杂度:O(n),其中 n 是输入 items 的长度。需要遍历一遍 items。

  • 空间复杂度:O(1),仅消耗常数空间。

 Comments
On this page
1773-统计匹配检索规则的物品数量