Leetcode 811. Subdomain Visit Count

Poby’s Home
Apr 18, 2021

--

easy, but string practice

There must be a better way, but I just wanted to practice string manipulation in C++.

Better Way

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;
}
};

--

--

No responses yet