Single-Condition Product Filter
Problem statement
A catalog has n products. Product i has the id ids[i], the category tags categoryTags[i], and the price prices[i].
One filter rule decides which products match. The rule has up to two criteria and an operator:
- Category criterion (
ruleCategoryTags): a product passes when at least one of its tags equals one of the rule's tags. Tags are compared without regard to letter case, so"ELECTRONICS"matches"electronics". An emptyruleCategoryTagsmeans the rule has no category criterion. - Price criterion (
minPriceandmaxPrice): a product passes when its price is at leastminPriceand at mostmaxPrice. Both bounds are inclusive. A negative bound is not set and is skipped. If both bounds are negative, the rule has no price criterion.
A criterion the rule does not set is not applicable and is ignored. ruleOperator is "AND" or "OR":
- With
"AND", a product matches when it passes every applicable criterion. - With
"OR", a product matches when it passes at least one applicable criterion. - If the rule has no applicable criterion, every product matches, whatever the operator.
Return the ids of the matching products in the same order as the input. Return an empty array when no product matches.
Function
filterProducts(ids: String[], categoryTags: String[][], prices: double[], ruleCategoryTags: String[], minPrice: double, maxPrice: double, ruleOperator: String) → String[]Examples
Example 1
ids = ["p1","p2","p3","p4","p5"]categoryTags = [["ELECTRONICS","HOME"],["KITCHEN"],["Books"],["home","garden"],[]]prices = [199.99,35.5,12.0,89.0,5.0]ruleCategoryTags = ["electronics","kitchen"]minPrice = 30.0maxPrice = 100.0ruleOperator = "AND"return = ["p2"]- Category criterion:
p1hasELECTRONICSandp2hasKITCHEN, which matchelectronicsandkitchenwhen case is ignored.p3,p4, andp5have no matching tag. - Price criterion: only
p2(35.5) andp4(89.0) are between 30.0 and 100.0. - With
"AND", onlyp2passes both.
Example 2
ids = ["p1","p2","p3","p4","p5"]categoryTags = [["ELECTRONICS","HOME"],["KITCHEN"],["Books"],["home","garden"],[]]prices = [199.99,35.5,12.0,89.0,5.0]ruleCategoryTags = ["electronics","kitchen"]minPrice = 30.0maxPrice = 100.0ruleOperator = "OR"return = ["p1","p2","p4"]The same rule with "OR". p1 passes the category criterion, p2 passes both, and p4 passes the price criterion. p3 and p5 pass neither. The ids keep the input order.
Example 3
ids = ["p1","p2","p3","p4","p5"]categoryTags = [["ELECTRONICS","HOME"],["KITCHEN"],["Books"],["home","garden"],[]]prices = [199.99,35.5,12.0,89.0,5.0]ruleCategoryTags = []minPrice = -1.0maxPrice = 35.5ruleOperator = "AND"return = ["p2","p3","p5"]The rule has no category criterion and no minimum price, so only price <= 35.5 is checked. The bound is inclusive, so p2 (35.5) matches, along with p3 (12.0) and p5 (5.0). The unset category criterion does not make products fail under "AND".
Constraints
1 <= n <= 500ids.length == categoryTags.length == prices.length == n1 <= ids[i].length <= 10, andids[i]contains only lowercase English letters and digits.0 <= categoryTags[i].length <= 40 <= ruleCategoryTags.length <= 5- Every tag in
categoryTagsandruleCategoryTagshas1to20characters, and every character is an English letter (AtoZoratoz). 0 <= prices[i] <= 10^6-1 <= minPrice, maxPrice <= 10^6. A negative value means that bound is not set.ruleOperatoris"AND"or"OR".