从给定的电子邮件列表中计算唯一的域名
- 最后更新: 2022年5月10日
给定一个包含N个不同域名的电子邮件地址的数组 arr[],任务是从电子邮件列表中找出唯一的域名和它们的出现频率。
**注意:**域名的出现必须按照域名的词法顺序打印。
例子。
**输入:**arr[] = { "rupesh@gmail.com", "akole@yahoo.com", "rupesh.21910879@viit.ac.in",
"faculty.surname@viit.ac.in", "Shyam@gmail.com", "examcell@viit.ac.in" }
**输出:**gmail.com 2
viit.ac.in 3
yahoo.com 1
说明。 在这里,从电子邮件列表中按词法顺序排列的独特域名是:
gmail.com, viit.ac.in, yahoo.com。
它们在列表中的频率分别是2、3和1。**输入:**arr[] = {"geeks@geeksforgeeks.org", "google@gmail.com"}
**输出:**geeksforgeeks.org 1
gmail.com 1
办法。这 个问题的解决方案是基于以下想法。
一封电子邮件的域名都是在'@'符号后面提到的。因此,找到所有的域并存储它们的频率。
按照下面提到的步骤来实现这个想法。
- 初始化一个 地图来存储每个独特域的频率,以域名的词法顺序。
- 遍历每个电子邮件条目。
- 对于每个条目,找到'@'符号的索引(例如idx)。
- 从idx+1开始直到字符串结束的子串就是域名。
- 将这个域名存储在地图中,并将其频率增加1。
- 从头开始迭代地图(因为键已经被排序),并打印域名和它们的频率。
下面是上述方法的实现。
C++
// C++ code to implement the approach#include <bits/stdc++.h>using namespace std;// Function to find the unique domains// and their frequenciesvector<pair<string,int> > finddomains(vector<string> input){// Map to store unique domains// and their frequencymap<string,int> domainFre;vector<pair<string,int> > ans;// Loop to find the unique domainsfor (int i = 0; i < input.size(); i++) {// Find the index of '@' symbol in stringauto findindex = input[i].find('@');// Push the subtring starting from// findindex + 1 till the end// and then increase its frequency by 1domainFre[input[i].substr(findindex + 1)]++;}// Store the key value pair into// vector and finally return it.for (auto it : domainFre)ans.push_back({ it.first, it.second });// Return the domains and their frequenciesreturn ans;}// Driver codeint main(){vector<string> input = {"rupesh@gmail.com","akole@yahoo.com","rupesh.21910879@viit.ac.in","faculty.surname@viit.ac.in","Shyam@gmail.com","examcell@viit.ac.in"};vector<pair<string,int> > ans;// Function callans = finddomains(input);// Print the unique domains and// their frequencies// in lexicographical orderfor (int i = 0; i < ans.size(); i++)cout << ans[i].first <<" "<< ans[i].second << endl;return 0;} |
输出
gmail.com 2
viit.ac.in 3
yahoo.com 1
时间复杂度。 O(N * M) 其中M是平均字符串大小
**辅助空间。**O(K) 其中K是唯一域的数量
我的个人笔记arrow_drop_up
保存