Tous les défis
Facile 0 résolus 0 % de réussite

Event Subscribers

Deliver events to the subscribers of each topic, observer style, and count what everyone receives.

Ce que vous allez apprendre

  • Map topics to the people who follow them
  • Notify every subscriber of an event
  • Ignore duplicate subscriptions with a set

Problème

Afficher la traduction

The observer pattern lets objects subscribe to a subject and get notified whenever something happens. Here, people subscribe to topics and every published event goes to everyone subscribed to its topic.

Each subscription is <person> <topic>. A repeated subscription has no extra effect. Then each event in events is a topic name: every subscriber of that topic receives one notification. Events on topics nobody follows are dropped.

Implement event_subscribers(subscriptions, events).

Input

subscriptions: "<person> <topic>" strings (names have no spaces).

events: Topic names, in publishing order.

Output

Return "<person>:<count>" for every person who has at least one subscription, sorted by name (ordinal character order).

Contraintes

1 ≤ number of subscriptions ≤ 10,000

0 ≤ number of events ≤ 100,000

Exemples

Exemple 1
Entrée :
events = ["news", "sports", "weather"]
subscriptions = ["bob sports", "alice news", "alice sports"]

Sortie attendue :
expected = ["alice:2", "bob:1"]
Entrée :
std::vector<std::string> events = {"news", "sports", "weather"};
std::vector<std::string> subscriptions = {"bob sports", "alice news", "alice sports"};

Sortie attendue :
std::vector<std::string> expected = {"alice:2", "bob:1"};
Entrée :
string[] events = new string[] {"news", "sports", "weather"};
string[] subscriptions = new string[] {"bob sports", "alice news", "alice sports"};

Sortie attendue :
string[] expected = new string[] {"alice:2", "bob:1"};

Explication

alice follows news and sports, so she gets both events; bob only gets sports; nobody follows weather: ["alice:2", "bob:1"].

Exemple 2
Entrée :
events = []
subscriptions = ["zoe tech", "zoe tech"]

Sortie attendue :
expected = ["zoe:0"]
Entrée :
std::vector<std::string> events = {};
std::vector<std::string> subscriptions = {"zoe tech", "zoe tech"};

Sortie attendue :
std::vector<std::string> expected = {"zoe:0"};
Entrée :
string[] events = new string[] {};
string[] subscriptions = new string[] {"zoe tech", "zoe tech"};

Sortie attendue :
string[] expected = new string[] {"zoe:0"};

Explication

The repeated subscription counts once, and no events were published: ["zoe:0"].

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Group subscribers by topic so each event finds its audience quickly.

Indice 2

Use a set per topic so a repeated subscription counts once.

Indice 3

Everyone who subscribed appears in the result, even with zero notifications.

Complexité attendue

O(subscriptions + events × subscribers per topic) time, O(subscriptions) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.