Skip to content

Commit 7bc04aa

Browse files
committed
New Problem Solution - "Count Items Matching a Rule"
1 parent 2e7a56e commit 7bc04aa

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ LeetCode
1515
|1781|[Sum of Beauty of All Substrings](https://leetcode.com/problems/sum-of-beauty-of-all-substrings/) | [C++](./algorithms/cpp/sumOfBeautyOfAllSubstrings/SumOfBeautyOfAllSubstrings.cpp)|Medium|
1616
|1780|[Check if Number is a Sum of Powers of Three](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/) | [C++](./algorithms/cpp/checkIfNumberIsASumOfPowersOfThree/CheckIfNumberIsASumOfPowersOfThree.cpp)|Medium|
1717
|1779|[Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/) | [C++](./algorithms/cpp/findNearestPointThatHasTheSameXOrYCoordinate/FindNearestPointThatHasTheSameXOrYCoordinate.cpp)|Easy|
18+
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/) | [C++](./algorithms/cpp/countItemsMatchingARule/CountItemsMatchingARule.cpp)|Easy|
1819
|1763|[Longest Nice Substring](https://leetcode.com/problems/longest-nice-substring/) | [C++](./algorithms/cpp/longestNiceSubstring/LongestNiceSubstring.cpp)|Easy|
1920
|1761|[Minimum Degree of a Connected Trio in a Graph](https://leetcode.com/problems/minimum-degree-of-a-connected-trio-in-a-graph/) | [C++](./algorithms/cpp/minimumDegreeOfAConnectedTrioInAGraph/MinimumDegreeOfAConnectedTrioInAGraph.cpp)|Hard|
2021
|1760|[Minimum Limit of Balls in a Bag](https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/) | [C++](./algorithms/cpp/minimumLimitOfBallsInABag/MinimumLimitOfBallsInABag.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Source : https://leetcode.com/problems/count-items-matching-a-rule/
2+
// Author : Hao Chen
3+
// Date : 2021-03-14
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given an array items, where each items[i] = [typei, colori, namei] describes the type,
8+
* color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and
9+
* ruleValue.
10+
*
11+
* The ith item is said to match the rule if one of the following is true:
12+
*
13+
* ruleKey == "type" and ruleValue == typei.
14+
* ruleKey == "color" and ruleValue == colori.
15+
* ruleKey == "name" and ruleValue == namei.
16+
*
17+
* Return the number of items that match the given rule.
18+
*
19+
* Example 1:
20+
*
21+
* Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]],
22+
* ruleKey = "color", ruleValue = "silver"
23+
* Output: 1
24+
* Explanation: There is only one item matching the given rule, which is
25+
* ["computer","silver","lenovo"].
26+
*
27+
* Example 2:
28+
*
29+
* Input: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]],
30+
* ruleKey = "type", ruleValue = "phone"
31+
* Output: 2
32+
* Explanation: There are only two items matching the given rule, which are ["phone","blue","pixel"]
33+
* and ["phone","gold","iphone"]. Note that the item ["computer","silver","phone"] does not match.
34+
*
35+
* Constraints:
36+
*
37+
* 1 <= items.length <= 104
38+
* 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
39+
* ruleKey is equal to either "type", "color", or "name".
40+
* All strings consist only of lowercase letters.
41+
******************************************************************************************************/
42+
43+
class Solution {
44+
public:
45+
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
46+
int idx;
47+
switch(ruleKey[0]) {
48+
case 't': idx = 0; break;
49+
case 'c': idx = 1; break;
50+
case 'n': idx = 2; break;
51+
default: return 0;
52+
}
53+
54+
int cnt = 0;
55+
for (auto& item : items) {
56+
if (item[idx] == ruleValue) cnt++;
57+
}
58+
return cnt;
59+
}
60+
};

0 commit comments

Comments
 (0)