From bfc780c4715383663f5a5e45fad2d2b1cf27015f Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Sun, 15 Mar 2026 10:20:45 +0800 Subject: [PATCH] Implement CountItemsMatchingARule1773 with complexity analysis and tests - Move source file to proper package structure (src/main/java/com/leetcode/easy/) - Implement countMatches method with O(n) time and O(1) space complexity - Add Javadoc complexity analysis following project conventions - Add JUnit tests with example cases Algorithm: Single pass through items list, checking ruleKey against "type", "color", or "name" and matching against ruleValue. --- src/CountItemsMatchingARule1773.java | 32 ------------- .../easy/CountItemsMatchingARule1773.java | 31 +++++++++++++ .../easy/CountItemsMatchingARule1773Test.java | 45 +++++++++++++++++++ 3 files changed, 76 insertions(+), 32 deletions(-) delete mode 100644 src/CountItemsMatchingARule1773.java create mode 100644 src/main/java/com/leetcode/easy/CountItemsMatchingARule1773.java create mode 100644 src/test/java/com/leetcode/easy/CountItemsMatchingARule1773Test.java diff --git a/src/CountItemsMatchingARule1773.java b/src/CountItemsMatchingARule1773.java deleted file mode 100644 index 01ee50e..0000000 --- a/src/CountItemsMatchingARule1773.java +++ /dev/null @@ -1,32 +0,0 @@ -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class CountItemsMatchingARule1773 { - public int countMatches(List> items, String ruleKey, String ruleValue) { - int res = 0; - Map mapping = new HashMap<>(); - mapping.put("type", 0); - mapping.put("color", 1); - mapping.put("name", 2); - int index = mapping.get(ruleKey); - for (List item : items) { - if (item.get(index).equals(ruleValue)) { - res++; - } - } - return res; - } - - public static void main(String[] args) { - List> items = new ArrayList<>(); - items.add(List.of("phone", "blue", "pixel")); - items.add(List.of("computer", "silver", "lenovo")); - items.add(List.of("phone", "gold", "iphone")); - String ruleKey = "color"; - String ruleValue = "silver"; - // 1 - System.out.println(new CountItemsMatchingARule1773().countMatches(items, ruleKey, ruleValue)); - } -} diff --git a/src/main/java/com/leetcode/easy/CountItemsMatchingARule1773.java b/src/main/java/com/leetcode/easy/CountItemsMatchingARule1773.java new file mode 100644 index 0000000..53e83fc --- /dev/null +++ b/src/main/java/com/leetcode/easy/CountItemsMatchingARule1773.java @@ -0,0 +1,31 @@ +// Tags: Array, String +package com.leetcode.easy; + +import java.util.List; + +public class CountItemsMatchingARule1773 { + /** + * Let n = items.size(). + * Time complexity: O(n) + * Breakdown: + * - Single for loop: Iterates through all items - O(n) + * - String comparison: O(1) per comparison (comparing with fixed ruleKey and ruleValue) + * Overall: O(n) + *

+ * Space complexity: O(1) + * - Only uses a single integer variable 'res' - O(1) + */ + public int countMatches(List> items, String ruleKey, String ruleValue) { + int res = 0; + for (List item : items) { + if (ruleKey.equals("type") && item.get(0).equals(ruleValue)) { + res++; + } else if (ruleKey.equals("color") && item.get(1).equals(ruleValue)) { + res++; + } else if (ruleKey.equals("name") && item.get(2).equals(ruleValue)) { + res++; + } + } + return res; + } +} diff --git a/src/test/java/com/leetcode/easy/CountItemsMatchingARule1773Test.java b/src/test/java/com/leetcode/easy/CountItemsMatchingARule1773Test.java new file mode 100644 index 0000000..544042b --- /dev/null +++ b/src/test/java/com/leetcode/easy/CountItemsMatchingARule1773Test.java @@ -0,0 +1,45 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CountItemsMatchingARule1773Test { + private CountItemsMatchingARule1773 solution; + + @BeforeEach + void setUp() { + solution = new CountItemsMatchingARule1773(); + } + + @Test + void testCountMatches_Example1() { + var items = List.of( + List.of("phone", "blue", "pixel"), + List.of("computer", "silver", "lenovo"), + List.of("phone", "gold", "iphone") + ); + String ruleKey = "color"; + String ruleValue = "silver"; + int expected = 1; + int result = solution.countMatches(items, ruleKey, ruleValue); + assertEquals(expected, result); + } + + @Test + void testCountMatches_Example2() { + var items = List.of( + List.of("phone", "blue", "pixel"), + List.of("computer", "silver", "lenovo"), + List.of("phone", "gold", "iphone") + ); + String ruleKey = "type"; + String ruleValue = "phone"; + int expected = 2; + int result = solution.countMatches(items, ruleKey, ruleValue); + assertEquals(expected, result); + } +}