#C12692. Group Emails by Domain
Group Emails by Domain
Group Emails by Domain
You are given a list of email addresses, some of which may be invalid. An email address is considered valid if and only if it contains exactly one '@' symbol, and both the username (the part before '@') and the domain (the part after '@') are non-empty. Your task is to process the given emails and group the valid ones by their domain.
For each valid email, extract the username and insert it into a group corresponding to its domain. Finally, output the result as a JSON formatted dictionary where the keys are the domain names and the values are lists of usernames in the order they appear in the input.
inputFormat
The first line of input contains an integer n, representing the number of email addresses. The following n lines each contain one email address.
outputFormat
Print a single line containing a JSON object. The JSON object maps each domain (string) to a list of usernames (array of strings) that belong to that domain. Only include valid email addresses.## sample
7
alice@example.com
bob@example.net
carol@example.com
dave@example.org
eve@example.net
invalid-email
frank@@example.com
{"example.com": ["alice", "carol"], "example.net": ["bob", "eve"], "example.org": ["dave"]}