forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subdomain_Visit_Count.cpp
30 lines (29 loc) · 1.01 KB
/
Subdomain_Visit_Count.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
unordered_map<string, int> visitFreq;
for(string cpDomain : cpdomains) {
int spacePos = cpDomain.find(" ");
int noOfVisit = atoi(cpDomain.substr(0, spacePos).c_str());
string domain = cpDomain.substr(spacePos + 1);
vector<string> subDomains;
while(true) {
subDomains.push_back(domain);
int dotPos = domain.find(".");
if(dotPos == string::npos) {
dotPos = domain.length();
break;
}
domain = domain.substr(dotPos + 1);
}
for(string subDomain : subDomains) {
visitFreq[subDomain] += noOfVisit;
}
}
vector<string> result;
for(auto entry : visitFreq) {
result.push_back(to_string(entry.second) + " " + entry.first);
}
return result;
}
};