모든 챌린지
쉬움 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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.