0%

811. Subdomain Visit Count

O(n) time O(n) space

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
unordered_map<string, int> m;
int cnt = 0;
string d;
for (const auto &s : cpdomains) {
istringstream input(s);
input >> cnt >> d;
m[d] += cnt;
int n = d.length();
for (int i = 0; i < n; ++i) {
if (d[i] == '.') {
m[d.substr(i + 1)] += cnt;
}
}
}
vector<string> res;
for (const auto &p : m) {
res.push_back(to_string(p.second) + " " + p.first);
}
return res;
}
};