Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a6957bb

Browse files
authored
Optimal Solution
Runtime: 3 ms, faster than 98.33% of Java online submissions for Count Items Matching a Rule. Memory Usage: 47 MB, less than 92.77% of Java online submissions for Count Items Matching a Rule.
1 parent 7933847 commit a6957bb

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/main/java/com/fishercoder/solutions/_1773.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
public class _1773 {
66
public static class Solution1 {
77
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
8+
int index =0;
9+
if(ruleKey.equals("color")){
10+
index =1;
11+
}
12+
if(ruleKey.equals("name")){
13+
index =2;
14+
}
15+
816
int match = 0;
17+
18+
919
for (List<String> item : items) {
10-
if (ruleKey.equals("type") && item.get(0).equals(ruleValue)) {
11-
match++;
12-
} else if (ruleKey.equals("color") && item.get(1).equals(ruleValue)) {
13-
match++;
14-
} else if (ruleKey.equals("name") && item.get(2).equals(ruleValue)) {
20+
if(item.get(index).equals(ruleValue)){
1521
match++;
1622
}
1723
}

0 commit comments

Comments
 (0)