Problem
C# records compare by value: two records are equal when all their fields are equal, even if they are different objects. That makes removing duplicates easy.
Each record is written name,city. Trim spaces around both fields; after trimming, two records are equal when both fields match exactly (case matters).
Implement unique_records(records).
Input
records: The records, in order.
Output
Return the distinct records in the order they first appear, written name,city with the trimmed fields.
Einschränkungen
0 ≤ number of records ≤ 100,000
Each field has 1 to 30 characters and no commas
Beispiele
Eingabe:
records = ["Ada,London", "Lin, Hanoi", "Ada , London"]
Erwartete Ausgabe:
expected = ["Ada,London", "Lin,Hanoi"]
Eingabe:
std::vector<std::string> records = {"Ada,London", "Lin, Hanoi", "Ada , London"};
Erwartete Ausgabe:
std::vector<std::string> expected = {"Ada,London", "Lin,Hanoi"};
Eingabe:
string[] records = new string[] {"Ada,London", "Lin, Hanoi", "Ada , London"};
Erwartete Ausgabe:
string[] expected = new string[] {"Ada,London", "Lin,Hanoi"};
Erläuterung
After trimming, the third record equals the first, so it is dropped: ["Ada,London", "Lin,Hanoi"].
Eingabe:
records = ["ada,Paris", "Ada,Paris"]
Erwartete Ausgabe:
expected = ["ada,Paris", "Ada,Paris"]
Eingabe:
std::vector<std::string> records = {"ada,Paris", "Ada,Paris"};
Erwartete Ausgabe:
std::vector<std::string> expected = {"ada,Paris", "Ada,Paris"};
Eingabe:
string[] records = new string[] {"ada,Paris", "Ada,Paris"};
Erwartete Ausgabe:
string[] expected = new string[] {"ada,Paris", "Ada,Paris"};
Erläuterung
Case matters, so these two records are different: ["ada,Paris", "Ada,Paris"].
So gehst du vor
Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.
Hinweis 1
Trim both fields before comparing records.
Hinweis 2
A set of (name, city) pairs remembers which records you have seen.
Hinweis 3
Output each new record the first time it appears, in input order.
Erwartete Komplexität
O(total length) time, O(number of records) extra space
Referenzen
Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.