forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subdomain-visit-count.cpp
28 lines (26 loc) · 947 Bytes
/
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
// Time: O(n), is the length of cpdomains (assuming the length of cpdomains[i] is fixed)
// Space: O(n)
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
unordered_map<string, int> counts;
for (const auto& cpdomain : cpdomains) {
int i = cpdomain.find(" ");
const int count = stoi(cpdomain.substr(0, i));
const auto& domain = cpdomain.substr(i + 1);
for (int i = -1; i < static_cast<int>(domain.length()); ++i) {
if (i != -1 && domain[i] != '.') {
continue;
}
counts[domain.substr(i + 1)] += count;
}
}
vector<string> result;
for (const auto& count : counts) {
result.emplace_back(to_string(count.second));
result.back() += " ";
result.back() += count.first;
}
return result;
}
};