문제
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
예시
입력:
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"].
입력:
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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.