Einfach 0 gelöst 0% Erfolgsquote

Event Subscribers

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

Das lernst du

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

Problem

Übersetzung anzeigen

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).

Einschränkungen

1 ≤ number of subscriptions ≤ 10,000

0 ≤ number of events ≤ 100,000

Beispiele

Beispiel 1
Eingabe:
subscriptions = ["bob sports", "alice news", "alice sports"]
events = ["news", "sports", "weather"]

Erwartete Ausgabe:
expected = ["alice:2", "bob:1"]
Eingabe:
std::vector<std::string> subscriptions = {"bob sports", "alice news", "alice sports"};
std::vector<std::string> events = {"news", "sports", "weather"};

Erwartete Ausgabe:
std::vector<std::string> expected = {"alice:2", "bob:1"};
Eingabe:
string[] subscriptions = new string[] {"bob sports", "alice news", "alice sports"};
string[] events = new string[] {"news", "sports", "weather"};

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

Erläuterung

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

Beispiel 2
Eingabe:
subscriptions = ["zoe tech", "zoe tech"]
events = []

Erwartete Ausgabe:
expected = ["zoe:0"]
Eingabe:
std::vector<std::string> subscriptions = {"zoe tech", "zoe tech"};
std::vector<std::string> events = {};

Erwartete Ausgabe:
std::vector<std::string> expected = {"zoe:0"};
Eingabe:
string[] subscriptions = new string[] {"zoe tech", "zoe tech"};
string[] events = new string[] {};

Erwartete Ausgabe:
string[] expected = new string[] {"zoe:0"};

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

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

Hinweis 2

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

Hinweis 3

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

Erwartete Komplexität

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.