Leetcode: Count Items Matching a Rule (Kotlin)
Leetcode: Count Items Matching a Rule (Kotlin)
This is an easy array problem on Leetcode. Count Items Matching a Rule - LeetCode
Problem Statement
Examples
Constraints
Understanding the Problem
You should use the given rule key to determine which index in each list you should check (“type” == 0, “color” == 1, “name == 2” . You will then check the value at the index in each list to the given rule value. You count each time these values match.
Approach #1
The first way I solved this was by using a map that had a string as the key and the values as integers. This way, I could use the given rule key to get the given rule index. Then I loop through each list to find matches. I then returned the number of matches. {% gist https://gist.github.com/cmcoffeedev/5910fe88cb47fa46f2b47f6dcb2cc365.js %}
Approach #2:
Approach number 2 would use a when statement and assign that to an immutable value. {% gist https://gist.github.com/cmcoffeedev/034f96e6735b2bfb8e519d3a0d64d86c.js %}