Count Items Matching a Rule

🏠 ⬅️ ➡️

You are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ruleValue.

The ith item is said to match the rule if one of the following is true:

  • ruleKey == "type " and ruleValue == typei.
  • ruleKey == "color " and ruleValue == colori.
  • ruleKey == "name " and ruleValue == namei.

Return the number of items that match the given rule.

Example 1:

Input: items = [[ "phone ", "blue ", "pixel "],[ "computer ", "silver ", "lenovo "],[ "phone ", "gold ", "iphone "]], ruleKey = "color ", ruleValue = "silver " Output: 1 Explanation: There is only one item matching the given rule, which is [ "computer ", "silver ", "lenovo "].

Example 2:

Input: items = [[ "phone ", "blue ", "pixel "],[ "computer ", "silver ", "phone "],[ "phone ", "gold ", "iphone "]], ruleKey = "type ", ruleValue = "phone " Output: 2 Explanation: There are only two items matching the given rule, which are [ "phone ", "blue ", "pixel "] and [ "phone ", "gold ", "iphone "]. Note that the item [ "computer ", "silver ", "phone "] does not match.

Constraints:

  • 1 <= items.length <= 104
  • 1 <= typei.length, colori.length, namei.length, ruleValue.length <= 10
  • ruleKey is equal to either "type ", "color ", or "name ".
  • All strings consist only of lowercase letters.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i, j, n, m
    character(len=10) :: ruleKey, ruleValue
    character(len=10), allocatable :: items(:, :)
    integer :: numMatchingItems

    ! read input
    read(*,*) n
    allocate(items(n,3))
    do i = 1, n
        read(*,*) items(i,:)
    end do
    read(*,*) ruleKey
    read(*,*) ruleValue

    ! solve problem
    numMatchingItems = countMatchingItems(items, ruleKey, ruleValue)

    ! print output
    write(*,*) numMatchingItems

contains

    function countMatchingItems(items, ruleKey, ruleValue) result(numMatchingItems)
        implicit none
        integer :: i, j, n, m
        character(len=10), intent(in) :: ruleKey, ruleValue
        character(len=10), allocatable :: items(:, :)
        integer :: numMatchingItems

        n = size(items, 1)
        m = size(items, 2)
        numMatchingItems = 0
        do i = 1, n
            do j = 1, m
                if (ruleKey == "type " .and. ruleValue == items(i,j)) then
                    numMatchingItems = numMatchingItems + 1
                    exit
                else if (ruleKey == "color " .and. ruleValue == items(i,j)) then
                    numMatchingItems = numMatchingItems + 1
                    exit
                else if (ruleKey == "name " .and. ruleValue == items(i,j)) then
                    numMatchingItems = numMatchingItems + 1
                    exit
                end if
            end do
        end do
    end function countMatchingItems

end program main
Compiled
Executed
Correct
program count_matching_items
      implicit none

      integer, parameter :: MAX_ITEMS = 100000
      integer, parameter :: MAX_RULE_VALUE_LENGTH = 10

      character(len=10) :: items(MAX_ITEMS, 3)
      character(len=10) :: ruleKey
      character(len=MAX_RULE_VALUE_LENGTH) :: ruleValue

      integer :: numItems, numMatchingItems
      integer :: i

      read(*,*) numItems

      do i = 1, numItems
        read(*,*) items(i, 1), items(i, 2), items(i, 3)
      end do

      read(*,*) ruleKey, ruleValue

      numMatchingItems = 0
      do i = 1, numItems
        if (ruleKey == "type " .and. items(i, 1) == ruleValue) then
          numMatchingItems = numMatchingItems + 1
        else if (ruleKey == "color " .and. items(i, 2) == ruleValue) then
          numMatchingItems = numMatchingItems + 1
        else if (ruleKey == "name " .and. items(i, 3) == ruleValue) then
          numMatchingItems = numMatchingItems + 1
        end if
      end do

      write(*,*) numMatchingItems

      end program count_matching_items
🌐 Data from online sources
def countMatches(items, ruleKey, ruleValue):
    count = 0
    index = {"type": 0, "color": 1, "name": 2}[ruleKey]
    for item in items:
        if item[index] == ruleValue:
            count += 1
    return count

The algorithm first finds the index of the attribute to be compared based on the ruleKey. Then, it iterates over the items array and compares the attribute at the calculated index with ruleValue. If there is a match, it increments a count variable. Finally, it returns the count representing the number of items matching the given rule.

🌐 Data from online sources
#include <vector>
#include <string>
using namespace std;

int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
    int count = 0;
    int index = (ruleKey == "type" ? 0 : (ruleKey == "color" ? 1 : 2));
    for (const auto& item : items) {
        if (item[index] == ruleValue)
            count++;
    }
    return count;
}

The algorithm first finds the index of the attribute to be compared based on the ruleKey. Then, it iterates over the items array and compares the attribute at the calculated index with ruleValue. If there is a match, it increments a count variable. Finally, it returns the count representing the number of items matching the given rule.