{"id":8161,"date":"2021-02-27T20:25:28","date_gmt":"2021-02-28T04:25:28","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8161"},"modified":"2021-02-27T20:27:02","modified_gmt":"2021-02-28T04:27:02","slug":"leetcode-1773-count-items-matching-a-rule","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1773-count-items-matching-a-rule\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1773. Count Items Matching a Rule"},"content":{"rendered":"\n<p>You are given an array&nbsp;<code>items<\/code>, where each&nbsp;<code>items[i] = [type<sub>i<\/sub>, color<sub>i<\/sub>, name<sub>i<\/sub>]<\/code>&nbsp;describes the type, color, and name of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;item. You are also given a rule represented by two strings,&nbsp;<code>ruleKey<\/code>&nbsp;and&nbsp;<code>ruleValue<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;item is said to match the rule if&nbsp;<strong>one<\/strong>&nbsp;of the following is true:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>ruleKey == \"type\"<\/code>&nbsp;and&nbsp;<code>ruleValue == type<sub>i<\/sub><\/code>.<\/li><li><code>ruleKey == \"color\"<\/code>&nbsp;and&nbsp;<code>ruleValue == color<sub>i<\/sub><\/code>.<\/li><li><code>ruleKey == \"name\"<\/code>&nbsp;and&nbsp;<code>ruleValue == name<sub>i<\/sub><\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the number of items that match the given rule<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> items = [[\"phone\",\"blue\",\"pixel\"],[\"computer\",\"silver\",\"lenovo\"],[\"phone\",\"gold\",\"iphone\"]], ruleKey = \"color\", ruleValue = \"silver\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> There is only one item matching the given rule, which is [\"computer\",\"silver\",\"lenovo\"].\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> items = [[\"phone\",\"blue\",\"pixel\"],[\"computer\",\"silver\",\"phone\"],[\"phone\",\"gold\",\"iphone\"]], ruleKey = \"type\", ruleValue = \"phone\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are only two items matching the given rule, which are [\"phone\",\"blue\",\"pixel\"] and [\"phone\",\"gold\",\"iphone\"]. Note that the item [\"computer\",\"silver\",\"phone\"] does not match.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= items.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= type<sub>i<\/sub>.length, color<sub>i<\/sub>.length, name<sub>i<\/sub>.length, ruleValue.length &lt;= 10<\/code><\/li><li><code>ruleKey<\/code>&nbsp;is equal to either&nbsp;<code>\"type\"<\/code>,&nbsp;<code>\"color\"<\/code>, or&nbsp;<code>\"name\"<\/code>.<\/li><li>All strings consist only of lowercase letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {\n    return count_if(begin(items), end(items), [&](const auto& item) {\n      return (ruleKey == \"type\" && item[0] == ruleValue\n        || ruleKey == \"color\" && item[1] == ruleValue\n        || ruleKey == \"name\" && item[2] == ruleValue);\n    });    \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array&nbsp;items, where each&nbsp;items[i] = [typei, colori, namei]&nbsp;describes the type, color, and name of the&nbsp;ith&nbsp;item. You are also given a rule represented&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,4],"class_list":["post-8161","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8161"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8161\/revisions"}],"predecessor-version":[{"id":8163,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8161\/revisions\/8163"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}