Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Unique Records

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

0 ≤ number of records ≤ 100,000

Each field has 1 to 30 characters and no commas

Ví dụ

Ví dụ 1
Đầu vào:
records = ["Ada,London", "Lin, Hanoi", "Ada , London"]

Kết quả mong đợi:
expected = ["Ada,London", "Lin,Hanoi"]
Đầu vào:
std::vector<std::string> records = {"Ada,London", "Lin, Hanoi", "Ada , London"};

Kết quả mong đợi:
std::vector<std::string> expected = {"Ada,London", "Lin,Hanoi"};
Đầu vào:
string[] records = new string[] {"Ada,London", "Lin, Hanoi", "Ada , London"};

Kết quả mong đợi:
string[] expected = new string[] {"Ada,London", "Lin,Hanoi"};

Giải thích

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

Ví dụ 2
Đầu vào:
records = ["ada,Paris", "Ada,Paris"]

Kết quả mong đợi:
expected = ["ada,Paris", "Ada,Paris"]
Đầu vào:
std::vector<std::string> records = {"ada,Paris", "Ada,Paris"};

Kết quả mong đợi:
std::vector<std::string> expected = {"ada,Paris", "Ada,Paris"};
Đầu vào:
string[] records = new string[] {"ada,Paris", "Ada,Paris"};

Kết quả mong đợi:
string[] expected = new string[] {"ada,Paris", "Ada,Paris"};

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Trim both fields before comparing records.

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.