Filter Contacts by Email Domain
Problem statement
You are given contact rows and a requested email domain. Each contact row has the form name,email,phone.
Return every original contact row whose email has exactly one @, a non-empty local part, and a domain equal to the requested domain using case-insensitive comparison. The requested domain may optionally begin with @.
Preserve the contacts' input order. Ignore malformed contact rows and malformed email addresses.
Function
filterContactsByDomain(contacts: String[], domain: String) → String[]Examples
Example 1
contacts = ["Alice,alice@example.com,111","Bob,bob@other.com,222","Cara,cara@EXAMPLE.COM,333"]domain = "example.com"return = ["Alice,alice@example.com,111","Cara,cara@EXAMPLE.COM,333"]Alice and Cara have the requested domain. Domain comparison is case-insensitive, and their original rows and order are preserved.
Example 2
contacts = ["A,a@shop.io,1","B,b@sub.shop.io,2","C,invalid,3"]domain = "@shop.io"return = ["A,a@shop.io,1"]The optional leading @ is ignored. A subdomain is not an exact match, and the malformed email is ignored.
Example 3
contacts = ["A,@example.com,1","B,b@@example.com,2","C,c@example.org,3"]domain = "example.com"return = []The first email has an empty local part, the second has two @ characters, and the third uses another domain.
Constraints
0 <= contacts.length <= 100000.- Every contact string has length at most
500. - A well-formed contact row contains exactly two commas; its fields do not contain commas.
domainis non-empty after removing one optional leading@and contains no whitespace, comma, or@.- Email local parts and domains are compared without trimming; only domain letter case is ignored.