111. Connected records
Medium
A platform stores a list of records, each consisting of a name followed by one or more emails. Two records belong to the same person if they share an email, either directly or through a chain of connected records. All records belonging to the same person have the same name.
Merge the records for each person. In each result, place the name first, followed by all distinct emails in ascending lexicographic order. Sort the final list of results by the first email in each result, also in ascending lexicographic order. Emails are compared as strings.
Example 1
Input: contas = [["Ana", "ana@exemplo.com", "trabalho@exemplo.com"], ["Ana", "trabalho@exemplo.com", "ana2@exemplo.com"], ["Bia", "bia@exemplo.com"]]
Output: [["Ana", "ana2@exemplo.com", "ana@exemplo.com", "trabalho@exemplo.com"], ["Bia", "bia@exemplo.com"]]
Explanation: Ana's two records share an email, so they are merged.
Example 2
Input: contas = [["Caio", "z@exemplo.com", "a@exemplo.com"], ["Caio", "m@exemplo.com"], ["Duda", "b@exemplo.com", "b@exemplo.com"]]
Output: [["Caio", "a@exemplo.com", "z@exemplo.com"], ["Duda", "b@exemplo.com"], ["Caio", "m@exemplo.com"]]
Explanation: Caio's records do not share emails, and Duda's repeated email appears only once.
Constraints
- 1 <= contas.length <= 10^4
- Each record contains a name and at least one email.
- 1 <= total number of emails across all records <= 10^5
- Names and emails are non-empty strings.
- Emails contain only lowercase ASCII letters, digits, and the characters '.', '+', and '@'.
- Records connected through shared emails always have the same name.
Union-findHash mapStrings
Saved in this browser
Caso 1
contas =
[["Ana","ana@exemplo.com","trabalho@exemplo.com"],["Ana","trabalho@exemplo.com","ana2@exemplo.com"],["Bia","bia@exemplo.com"]]
Expected =
[["Ana","ana2@exemplo.com","ana@exemplo.com","trabalho@exemplo.com"],["Bia","bia@exemplo.com"]]
Caso 2
contas =
[["Caio","z@exemplo.com","a@exemplo.com"],["Caio","m@exemplo.com"],["Duda","b@exemplo.com","b@exemplo.com"]]
Expected =
[["Caio","a@exemplo.com","z@exemplo.com"],["Duda","b@exemplo.com"],["Caio","m@exemplo.com"]]
Run checks these 2 cases. Submit runs all 11, including hidden ones.