Thursday, May 19, 2022
HomeSoftware DevelopmentRely distinctive domains from given Checklist of Emails

Rely distinctive domains from given Checklist of Emails

Given an array arr[] containing N electronic mail addresses with completely different domains, the duty is to seek out the distinctive domains and their frequencies from the checklist of emails.

Notice:  The prevalence of the area should be printed within the lexicographical order of domains.


Enter: arr[] = { “”, “”, “”,  
                         “”, “”, “”} 
Output: 2 
Clarification:  Right here the distinctive domains in lexicographical order from the checklist of emails are:,,
And their respective frequecy within the checklist is 2, 3 and 1.

Enter: arr[] = {“”, “”}
Output: 1


Strategy: The answer to the issue is predicated on the next thought:

The domains of an electronic mail are talked about after the ‘@’ image. So, discover all of the domains and retailer their frequencies.

Comply with the steps talked about beneath to implement the thought:

  • Initialize a map to retailer the frequencies of every distinctive area within the lexicographical order of domains. 
  • Transverse every of the e-mail entries:
    • For every entry discover the index (say idx) of the ‘@’ image.
    • The substring ranging from idx+1 until the tip of the string is the area identify.
    • Retailer this area within the map and improve its frequency by 1.
  • Iterate the map from begin (as the important thing are already in sorted order) and print the domains and their frequencies.  

Under is the implementation of the above method.



#embody <bits/stdc++.h>

utilizing namespace std;


vector<pair<string, int> > finddomains(

    vector<string> enter)




    map<string, int> domainFre;

    vector<pair<string, int> > ans;



    for (int i = 0; i < enter.measurement(); i++) {



        auto findindex = enter[i].discover('@');





        domainFre[input[i].substr(findindex + 1)]++;





    for (auto it : domainFre)

        ans.push_back({ it.first, it.second });



    return ans;



int fundamental()


    vector<string> enter = {

        "", "",



        "", ""


    vector<pair<string, int> > ans;



    ans = finddomains(enter);





    for (int i = 0; i < ans.measurement(); i++)

        cout << ans[i].first << " "

             << ans[i].second << endl;

    return 0;


Output 2 3 1

Time Complexity: O(N * M) the place M is the typical string measurement
Auxiliary House: O(Okay) the place Okay is the variety of distinctive domains


Most Popular

Recent Comments