Leetcode 811. Subdomain Visit Count

class Solution {
unordered_map<string, int> hash;
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
for (int i=0; i<cpdomains.size(); i++) {
string curr = cpdomains[i];
size_t space= curr.find(' ');
int count = std::stoi(curr.substr(0, space));
string domain = curr.substr(space + 1);
while(domain != "") {
hash[domain] += count;
cout << domain << endl;
size_t pos = domain.find('.');
if (pos == string::npos) {
domain = "";
} else {
domain = domain.substr(pos + 1);
}
}
}

vector<string> answer;
for (auto const& [key, value]: hash) {
string temp = to_string(value) + " " + key;
answer.push_back(temp);
}
return answer;
}
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store