class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        """??O(N*W*L) /O(N*L)  L: longest length of subdomain; W: longest length of subdomain
        1. split domain into cnt and domain, note cnt need to be convert into int 
        2. further split domain into different subdomain
        3. loop through subdomain accumutively and update dictionary 
        
        logic ["9001 discuss.leetcode.com"] => discuss.leetcode.com:9001, leetcode.com:9001, com:9001 
        """
        d = {}
        for t_domain in cpdomains:
            t,domain = t_domain.split()
            dos = domain.split('.')
            for i in range(len(dos)):
                k = '.'.join(dos[i:]) # note need to use hashable key, can avoid split twice by skip '.'  from string as key 
                d[k] = d.get(k,0) + int(t) 
        
        return [f'{v} {k}' for k,v in d.items() ]

optimization from above

class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        """??O(N*W*L) /O(N*L)  L: longest length of subdomain; W: longest length of subdomain
        1. split domain into cnt and domain, note cnt need to be convert into int 
        2. further split domain into different subdomain
        3. loop through subdomain accumutively and update dictionary 
        
        logic ["9001 discuss.leetcode.com"] => discuss.leetcode.com:9001, leetcode.com:9001, com:9001 
        """
        d = {}
        for t_domain in cpdomains:
            t,domain = t_domain.split()
            d[domain] = d.get(domain,0) + int(t)
            for i in range(len(domain)):
                if domain[i] == '.':
                    k = domain[i+1:] # note need to use hashable key 
                    d[k] = d.get(k,0) + int(t)
        
        return [f'{v} {k}' for k,v in d.items() ]