Subdomain Visit Count


  • 0

    Click here to see the full article post


  • 0
    J

    d = defaultdict(int)

    for i in cpdomains:
    count, url = i.split()
    for u in url.split('.'):
    d[url] += int(count)
    url = url.replace(u + '.', '')
    print(d)


  • 0
    P
    class Solution {
        public List<String> subdomainVisits(String[] cpdomains) {
            
            Map<String,Integer> map = new HashMap<>();
            
            for (int i=0;i<cpdomains.length;i++){
                boolean firstTime = true;
                String orgString = cpdomains[i];
                String[] spaceSplitArray = orgString.split(" ");
                int count = Integer.parseInt(spaceSplitArray[0]);
                
                String[] dotSplitArray = spaceSplitArray[1].split("\\.");
                String myString = "";
                
                for (int j= (dotSplitArray.length)-1 ;j>=0 ;j--){
                    
                    if (firstTime){
                    myString = dotSplitArray[j]+myString;
                    firstTime=false;
                    }else {
                        myString = dotSplitArray[j]+"."+myString;
                    }
                    
                    if (map.containsKey(myString)){ 
                        int temp = map.get(myString);
                        map.put(myString,temp+count);                                        
                    }else {
                        map.put(myString,count);                                    
                    }                                            
                }                                                                                    
            }
            
            
            List<String> answer = new ArrayList<>();
            
            for (Map.Entry<String,Integer> he : map.entrySet()){
                
                String temp = he.getValue().toString() +" "+ he.getKey();
                answer.add(temp);
            }
            return answer;
        }
    }
    

  • 0
    M

    Does this work? it gives desired results. Put someone please review the quality of code. Thanks in advance.

    def count_subdomain(cpdomains):
        domain_count = {}
        for domain in cpdomains:
            domain_info=domain.split(' ')
            count_of_domain=int(domain_info[0])
            split_of_domain=domain_info[1].split('.')
            length_of_domain=len(split_of_domain)
            temp_str=" "
            for i in range(length_of_domain):
                if temp_str==" ":
                    temp_str=split_of_domain[length_of_domain-1-i]
                else:
                    temp_str=split_of_domain[length_of_domain-1-i]+"."+temp_str
                if temp_str in domain_count:
                    domain_count[temp_str]+=count_of_domain
                else:
                    domain_count[temp_str]=count_of_domain
        return ["{} {}".format(value, key) for key, value in domain_count.iteritems()]
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.