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
32 changes: 0 additions & 32 deletions src/CountItemsMatchingARule1773.java

This file was deleted.

31 changes: 31 additions & 0 deletions src/main/java/com/leetcode/easy/CountItemsMatchingARule1773.java
Original file line number Diff line number Diff line change
@@ -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)
* <p>
* Space complexity: O(1)
* - Only uses a single integer variable 'res' - O(1)
*/
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int res = 0;
for (List<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading