Skip to content

Latest commit

 

History

History
66 lines (60 loc) · 1.34 KB

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

File metadata and controls

66 lines (60 loc) · 1.34 KB

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

/*
 * @lc app=leetcode.cn id=1773 lang=typescript
 *
 * [1773] 统计匹配检索规则的物品数量
 */

// @lc code=start
function countMatches(items: string[][], ruleKey: string, ruleValue: string): number {} // @lc code=end

解法 1: 枚举

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
}

Case

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)
  },
)