/*
* @lc app=leetcode.cn id=1773 lang=typescript
*
* [1773] 统计匹配检索规则的物品数量
*/
// @lc code=start
function countMatches(items: string[][], ruleKey: string, ruleValue: string): number {} // @lc code=end
function countMatches(items: string[][], ruleKey: string, ruleValue: string): number {
let res = 0
for (let [t, c, n] of items) {
if (
(ruleKey === 'type' && ruleValue === t) ||
(ruleKey === 'color' && ruleValue === c) ||
(ruleKey === 'name' && ruleValue === n)
) {
res++
}
}
return res
}
test.each([
{
input: {
items: [
['phone', 'blue', 'pixel'],
['computer', 'silver', 'lenovo'],
['phone', 'gold', 'iphone'],
],
ruleKey: 'color',
ruleValue: 'silver',
},
output: 1,
},
{
input: {
items: [
['phone', 'blue', 'pixel'],
['computer', 'silver', 'phone'],
['phone', 'gold', 'iphone'],
],
ruleKey: 'type',
ruleValue: 'phone',
},
output: 2,
},
])(
'input: items = $input.items, ruleKey = $input.ruleKey, ruleValue = $input.ruleValue',
({ input: { items, ruleKey, ruleValue }, output }) => {
expect(countMatches(items, ruleKey, ruleValue)).toEqual(output)
},
)