ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Unique Records

Remove duplicate records that are equal by value, keeping the first of each in order.

สิ่งที่คุณจะได้เรียนรู้

  • Compare records by their values
  • Remove duplicates while keeping order
  • Normalise fields by trimming spaces

โจทย์

ดูฉบับแปล

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.

เงื่อนไข

0 ≤ number of records ≤ 100,000

Each field has 1 to 30 characters and no commas

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
records = ["Ada,London", "Lin, Hanoi", "Ada , London"]

ผลลัพธ์ที่คาดหวัง:
expected = ["Ada,London", "Lin,Hanoi"]
อินพุต:
std::vector<std::string> records = {"Ada,London", "Lin, Hanoi", "Ada , London"};

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"Ada,London", "Lin,Hanoi"};
อินพุต:
string[] records = new string[] {"Ada,London", "Lin, Hanoi", "Ada , London"};

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"Ada,London", "Lin,Hanoi"};

คำอธิบาย

After trimming, the third record equals the first, so it is dropped: ["Ada,London", "Lin,Hanoi"].

ตัวอย่างที่ 2
อินพุต:
records = ["ada,Paris", "Ada,Paris"]

ผลลัพธ์ที่คาดหวัง:
expected = ["ada,Paris", "Ada,Paris"]
อินพุต:
std::vector<std::string> records = {"ada,Paris", "Ada,Paris"};

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"ada,Paris", "Ada,Paris"};
อินพุต:
string[] records = new string[] {"ada,Paris", "Ada,Paris"};

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"ada,Paris", "Ada,Paris"};

คำอธิบาย

Case matters, so these two records are different: ["ada,Paris", "Ada,Paris"].

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Trim both fields before comparing records.

คำใบ้ 2

A set of (name, city) pairs remembers which records you have seen.

คำใบ้ 3

Output each new record the first time it appears, in input order.

ความซับซ้อนที่คาดหวัง

O(total length) time, O(number of records) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก