Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions Array/1773. Count Items Matching a Rule.py

This file was deleted.

Empty file.
33 changes: 33 additions & 0 deletions problems/easy/count_items_matching_a_rule_1773/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Tags: Array, String
from typing import List


class Solution:
def count_matches(self, items: List[List[str]], rule_key: str, rule_value: str) -> int:
"""
Count the number of items that match the given rule.

Args:
items: List of items where each item is [type, color, name]
rule_key: The key to match against ('type', 'color', or 'name')
rule_value: The value to match

Returns:
Number of items matching the rule

Time complexity: O(n) where n is the number of items
- Single loop through all items: O(n)
- Each iteration does constant time comparisons: O(1)

Space complexity: O(1)
- Only uses a single counter variable (res)
"""
res = 0
for item in items:
if rule_key == 'type' and item[0] == rule_value:
res += 1
elif rule_key == 'color' and item[1] == rule_value:
res += 1
elif rule_key == 'name' and item[2] == rule_value:
res += 1
return res
15 changes: 15 additions & 0 deletions problems/easy/count_items_matching_a_rule_1773/test_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest
from tests.base_test import BaseTestSolution
from .solution import Solution


class TestSolution(BaseTestSolution):
solution = Solution()

@pytest.mark.parametrize("method_name, items, rule_key, rule_value, expected, timeout", [
('count_matches', [["phone", "blue", "pixel"], ["computer", "silver", "lenovo"], ["phone", "gold", "iphone"]],
"color", "silver", 1, None),
('count_matches', [["phone", "blue", "pixel"], ["computer", "silver", "lenovo"], ["phone", "gold", "iphone"]],
"type", "phone", 2, None)])
def test_count_matches(self, method_name, items, rule_key, rule_value, expected, timeout):
self._run_test(self.solution, method_name, (items, rule_key, rule_value,), expected, timeout)
Loading