From 13986c7b62312b0c30d66940efb05e0c551ac523 Mon Sep 17 00:00:00 2001 From: Imran Sefat <39518388+ImranSefat@users.noreply.github.com> Date: Mon, 2 May 2022 13:13:07 +0600 Subject: [PATCH] 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. --- .../java/com/fishercoder/solutions/_1773.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/fishercoder/solutions/_1773.java b/src/main/java/com/fishercoder/solutions/_1773.java index 306b2642aa..9f1bb563b0 100644 --- a/src/main/java/com/fishercoder/solutions/_1773.java +++ b/src/main/java/com/fishercoder/solutions/_1773.java @@ -5,13 +5,19 @@ public class _1773 { public static class Solution1 { public int countMatches(List> items, String ruleKey, String ruleValue) { + int index =0; + if(ruleKey.equals("color")){ + index =1; + } + if(ruleKey.equals("name")){ + index =2; + } + int match = 0; + + for (List item : items) { - if (ruleKey.equals("type") && item.get(0).equals(ruleValue)) { - match++; - } else if (ruleKey.equals("color") && item.get(1).equals(ruleValue)) { - match++; - } else if (ruleKey.equals("name") && item.get(2).equals(ruleValue)) { + if(item.get(index).equals(ruleValue)){ match++; } }