12 / 205
LeetCode · EasyOson10 ball· ~10 daqiqa

Count Items Matching a Rule

Sizga items nomli massiv berilgan, bunda har bir items[i] = [typei, colori, namei] mos ravishda elementning turi, rangi va nomini ifodalaydi. Shuningdek, ikkita satr ruleKey va ruleValue berilgan.

Agar quyidagilardan biri to'g'ri bo'lsa, i-chi element qoidaga mos keladi deb hisoblanadi:

  • ruleKey == "type" va ruleValue == typei
  • ruleKey == "color" va ruleValue == colori
  • ruleKey == "name" va ruleValue == namei

Qoidaga mos keladigan elementlar sonini qaytaring.

example.go
func solve(items [][]string, ruleKey string, ruleValue string) int {
    // ...
}

Misollar

kod
Input: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
Output: 1
Tushuntirish: Faqat bitta element mos keladi: ["computer","silver","lenovo"].
kod
Input: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", ruleValue = "phone"
Output: 2
kod
Input: items = [["tv", "black", "sony"], ["tv", "white", "lg"]], ruleKey = "name", ruleValue = "lg"
Output: 1

Maslahatlar

  1. ruleKey qaysi ustunga (type/color/name) mos kelishini avval aniqlab oling, so'ng shu ustunni tekshiring.
  2. ruleKey ga qarab tekshiriladigan indeksni (0, 1 yoki 2) tanlang, so'ng har bir elementning shu indeksdagi qiymatini ruleValue bilan solishtirib mos kelganlarni sanang.