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 <= 1041 <= typei.length, colori.length, namei.length, ruleValue.length <= 10ruleKey is equal to either "type ", "color ", or "name ".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
At line 10 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7fa986313960 in ??? #1 0x7fa9863144d9 in ??? #2 0x7fa98656817b in ??? #3 0x7fa986561684 in ??? #4 0x7fa9865622aa in ??? #5 0x5c582dd6c54b in MAIN__ #6 0x5c582dd6c9ff in main
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
At line 14 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7f924f6c8960 in ??? #1 0x7f924f6c94d9 in ??? #2 0x7f924f91d17b in ??? #3 0x7f924f916684 in ??? #4 0x7f924f9172aa in ??? #5 0x5b96eb71a23c in MAIN__ #6 0x5b96eb71a62e in main
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.
#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.