diff --git a/Array/1773. Count Items Matching a Rule.py b/Array/1773. Count Items Matching a Rule.py deleted file mode 100644 index 8cb0288..0000000 --- a/Array/1773. Count Items Matching a Rule.py +++ /dev/null @@ -1,15 +0,0 @@ -# Array, String -class Solution: - def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int: - res = 0 - for item in items: - num = None - if ruleKey == 'type': - num = 0 - elif ruleKey == 'color': - num = 1 - else: - num = 2 - if item[num] == ruleValue: - res += 1 - return res diff --git a/problems/easy/count_items_matching_a_rule_1773/__init__.py b/problems/easy/count_items_matching_a_rule_1773/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/problems/easy/count_items_matching_a_rule_1773/solution.py b/problems/easy/count_items_matching_a_rule_1773/solution.py new file mode 100644 index 0000000..1f2833b --- /dev/null +++ b/problems/easy/count_items_matching_a_rule_1773/solution.py @@ -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 diff --git a/problems/easy/count_items_matching_a_rule_1773/test_solution.py b/problems/easy/count_items_matching_a_rule_1773/test_solution.py new file mode 100644 index 0000000..d0eb432 --- /dev/null +++ b/problems/easy/count_items_matching_a_rule_1773/test_solution.py @@ -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)