---
# System prepended metadata

title: 811\. Subdomain Visit Count

---

# 811\. Subdomain Visit Count

```python=
class Solution:
    def subdomainVisits(self, cpdomains: List[str]):
        dict_ = {}
        for item in cpdomains:
            count, domain_str = item.split(' ')
            domain_list = domain_str.split('.')
            
            key = domain_list[-1]  # highest domain name
            if key not in dict_:
                dict_[key] = 0
            dict_[key] += int(count)
            
            for ele in domain_list[-2::-1]:
                key = ele + '.' + key
                if key not in dict_:
                    dict_[key] = 0
                dict_[key] += int(count)

        res = []
        for key in dict_:
            res.append(str(dict_[key])+' '+key)
        
        return res
    
class Solution:
    def subdomainVisits(self, cpdomains: List[str]):
        counter = Counter()
        
        for number_domain_pair in cpdomains:
            number, domain_string = number_domain_pair.split()  # defautl as ' '
             
            number = int(number)  # need to ask if legal string for int()
            segments = domain_string.split('.')
            
            for i in range(len(segments)):
                # even if we grow one by one, will not be faster
                # python string immutable
                domain = '.'.join(segments[i:])  # N^2, N is tot num fo chars 
                counter[domain] += number
        result = []
        for domain, count in counter.items():
             result.append(str(count) + ' ' + domain)
        return result
    
```