You are given an array of Email IDs, the task is to return the lexicographically first email ID of each single domain.
Examples:
input: Emails[] = {“ajay@gmail.com”, “rahul@hotmail.com”, “saif@gmail.com”, “faheem@yahoo.com”, “sam@hotmail.com”}
Output: {“ajay@gmail.com”, “rahul@hotmail.com”, “faheem@yahoo.com”}
Explanation: All email IDs are of the total of 3 email domains in the input array so the output contains alphabetically the first email ID of each domain.input: Emails[] = {“ben@gmail.com”, “alex@gmail.com”, “fin@gmail.com”, “zeke@gmail.com”}
Output: {“alex@gmail.com”}
Explanation: Every email ID is of only one domain ie “gmail” so the email which is alphabetically first among all is returned.
Approach: Implement the idea below to solve the problem:
The idea is to sort the input emails first and then iterate over the input email. For every single email extract its domain name and map it with entire email address using a hashmap and while iterating check whether any email of current email Id’s domain is present or not. If no such email Id is present in hashmap then insert the current email Id.
Follow the steps mentioned below to implement the idea:
- Sort the Input Array and declare a hashmap that will have a string as key and value.
- Iterate over the array and check whether the current email’s domain is present in the hashmap or not.
- If yes then do nothing.
- Otherwise, insert the current email ID into the hashmap along with its domain name.
- Iterate over the Hashmap and populate the output array with its values.
Below is the implementation of the above approach.
C++
|
ajay@gmail.com rahul@hotmail.com faheem@yahoo.com
Time Complexity: O(N)
Auxiliary Space: O(N)
Related articles:
isotretinoin for sale online order isotretinoin 20mg generic zithromax drug
oral ventolin 2mg levothroid usa augmentin 1000mg for sale
modafinil 200mg generic zestril 10mg sale lopressor cost
prednisolone 20mg canada cheap prednisolone generic cost lasix 40mg
buy avodart 0.5mg xenical 120mg price xenical 120mg tablet