forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubdomain Visit Count.cpp
39 lines (36 loc) · 968 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
29
30
31
32
33
34
35
36
37
38
39
// Runtime: 36 ms (Top 16.60%) | Memory: 11.7 MB (Top 62.43%)
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
vector<string> v;
unordered_map<string,int> m;
for(auto it : cpdomains){
string rep="";
int i=0;
while(it[i]!=' '){
rep+=(it[i]);
i++;
}
int r=stoi(rep);
m[it.substr(i+1,it.size())]+=r;
while(it[i]!='.'){
i++;
}
m[it.substr(i+1,it.size())]+=r;
i++;
while(i<it.size() and it[i]!='.'){
i++;
}
if(i!=it.size()){
m[it.substr(i+1,it.size())]+=r;
}
}
for(auto it : m){
string a=it.first;
string b=to_string(it.second);
a=b+" "+a;
v.push_back(a);
}
return v;
}
};