LeetCode 题解工作台

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

给你一个数组 items ,其中 items[i] = [type i , color i , name i ] ,描述第 i 件物品的类型、颜色以及名称。 另给你一条由两个字符串 ruleKey 和 ruleValue 表示的检索规则。 如果第 i 件物品能满足下述条件之一,则认为该物品与给定的检…

category

2

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·string

bolt

答案摘要

由于 `ruleKey` 只可能是 `"type"`、`"color"` 或 `"name"`,我们可以直接取 `ruleKey` 的第一个字符来确定 `item` 的下标 。然后遍历 `items` 数组,统计 `item[i] == ruleValue` 的个数即可。 时间复杂度 ,空间复杂度 。其中 为 `items` 的长度。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·string 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个数组 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"
  • 所有字符串仅由小写字母组成
lightbulb

解题思路

方法一:计数模拟

由于 ruleKey 只可能是 "type""color""name",我们可以直接取 ruleKey 的第一个字符来确定 item 的下标 ii。然后遍历 items 数组,统计 item[i] == ruleValue 的个数即可。

时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。其中 nnitems 的长度。

1
2
3
4
5
class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
        i = 0 if ruleKey[0] == 't' else (1 if ruleKey[0] == 'c' else 2)
        return sum(v[i] == ruleValue for v in items)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidate should understand how to iterate over arrays and conditionally check string values.

  • question_mark

    Look for the ability to optimize iterations, such as reducing unnecessary string comparisons.

  • question_mark

    See if the candidate can efficiently handle larger input sizes through improved algorithms.

warning

常见陷阱

外企场景
  • error

    Failing to properly match ruleKey to the correct index in the item array, leading to incorrect results.

  • error

    Not handling edge cases like empty arrays or rules that don't match any item.

  • error

    Unnecessarily complex solutions that don't take advantage of simple iteration for this problem.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if the ruleKey is dynamic and changes per item, requiring different ruleKey checks for each one?

  • arrow_right_alt

    Consider a variant where you need to count multiple types of rules in one pass, instead of just one rule.

  • arrow_right_alt

    Change the input so that the ruleValue is not a string but a more complex data structure, like a range or set.

help

常见问题

外企场景

统计匹配检索规则的物品数量题解:数组·string | LeetCode #1773 简单