-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-SubdomainVisitCount.cpp
More file actions
38 lines (28 loc) · 961 Bytes
/
LeetCode-SubdomainVisitCount.cpp
File metadata and controls
38 lines (28 loc) · 961 Bytes
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
31
32
33
34
35
36
37
38
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
map<string, int> m;
for (const string& s: cpdomains) {
string num, dom;
bool after = false;
for (const char& c: s)
if (after) dom += c;
else
if (c == ' ') after = true;
else num += c;
int n = stoi(num);
string d;
for (int i = dom.size() - 1; i >= 0; --i) {
if (dom[i] == '.') m[d] += n;
d += dom[i];
}
m[d] += n;
}
vector<string> v;
for (pair<string, int> e: m) {
reverse(e.first.begin(), e.first.end());
v.push_back(to_string(e.second) + " " + e.first);
}
return v;
}
};