Skip to content

Commit 9b576d6

Browse files
solves count items matching a rule
1 parent 823a7a1 commit 9b576d6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@
432432
| 1758 | [Minimum Changes To Make Alternating Binary String](https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string) | [![Java](assets/java.png)](src/MinimumChangesToMakeAlternatingBinaryString.java) | |
433433
| 1763 | [Longest Nice Substring](https://leetcode.com/problems/longest-nice-substring) | [![Java](assets/java.png)](src/LongestNiceSubstring.java) | |
434434
| 1768 | [Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately) | [![Java](assets/java.png)](src/MergeStringsAlternately.java) | |
435-
| 1773 | [Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule) | | |
435+
| 1773 | [Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule) | [![Java](assets/java.png)](src/CountItemsMatchingARule.java) | |
436436
| 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) | | |
437437
| 1784 | [Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones) | | |
438438
| 1790 | [Check if One String Swap Can Make Strings Equal](https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal) | | |

src/CountItemsMatchingARule.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.List;
2+
3+
public class CountItemsMatchingARule {
4+
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
5+
return countMatches(items, getRuleIndex(ruleKey), ruleValue);
6+
}
7+
8+
private int countMatches(List<List<String>> items, int ruleIndex, String ruleValue) {
9+
int matches = 0;
10+
for (List<String> item : items) {
11+
if (item.get(ruleIndex).equals(ruleValue)) matches++;
12+
}
13+
return matches;
14+
}
15+
16+
private int getRuleIndex(String key) {
17+
return switch (key) {
18+
case "type" -> 0;
19+
case "color" -> 1;
20+
case "name" -> 2;
21+
default -> -1;
22+
};
23+
}
24+
}

0 commit comments

Comments
 (0)