A farmers' market keeps a running ledger of every sale: each entry records the date it happened and the name of the product sold. The same product can be sold — and logged — more than once on the same date, and the ledger's entries are not necessarily written down in date order. For every date on which at least one sale was recorded, summarize that date by counting how many distinct products were sold on it and listing those distinct product names.
The first line contains an integer n, the number of ledger entries. Each of the next n lines contains one entry: a date token followed by a single space and a product-name token, e.g. 2020-05-30 Headphone. The date token is a string of at most 10 characters with no spaces (dates written in the same fixed-width format compare equal as strings exactly when they are the same calendar day, and comparing date tokens as strings also sorts them into chronological order). The product-name token is a string of 1 to 20 characters made up of letters, digits, underscores (_), and hyphens (-), with no spaces, and product names are case-sensitive (e.g. Apple and apple are different products).
Print one line for every distinct date that appears in the ledger, in ascending (chronological) order of the date token. Each line has the form:
date count product1,product2,...,productK
where count is the number of distinct products sold on that date and product1,...,productK are those distinct product names listed in ascending lexicographic order, separated by commas with no spaces.
Example 1
Input
8 2020-05-30 Headphone 2020-06-01 Pencil 2020-06-01 Mask 2020-06-02 Mask 2020-05-30 Basketball 2020-06-01 Bible 2020-06-02 Mask 2020-05-30 T-Shirt
Expected
2020-05-30 3 Basketball,Headphone,T-Shirt 2020-06-01 3 Bible,Mask,Pencil 2020-06-02 1 Mask
Explanation
On 2020-05-30 three distinct products were sold — Headphone, Basketball, T-Shirt — sorted as Basketball,Headphone,T-Shirt. On 2020-06-01 three distinct products were sold — Pencil, Mask, Bible — sorted as Bible,Mask,Pencil. On 2020-06-02 only Mask was sold (twice, but it counts once). The three dates are printed in ascending order.
Example 2
Input
4 2021-01-01 Apple 2021-01-01 Apple 2021-01-01 Banana 2021-01-02 Cherry
Expected
2021-01-01 2 Apple,Banana 2021-01-02 1 Cherry
Explanation
On 2021-01-01, Apple was sold twice and Banana once, giving 2 distinct products, sorted as Apple,Banana. On 2021-01-02 only Cherry was sold, giving 1 distinct product. The two dates are printed in ascending order.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →