You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A website domain "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com" and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.
/** * @param {string[]} cpdomains * @return {string[]} *//*visitCounts = {'google.mail.com': '900','yahoo.com' : 50,'intel.mail.com' : 1}so, visitCounts.key is google.mail.com, yahoo.com... value is 900, 50 .....Object is not Array, they work in a totally different way. visitCount[subdomain] means to add subdomain in visitCount object*/varsubdomainVisits=function(cpdomains){//1st. create an empty objectletvisitCount={};//2nd, traverse the given array, and split number of visit and domain, then storing them.for(leti=0;i<cpdomains.length;i++){let[visit,domains]=cpdomains[i].split(" ");//3rd, we further split subdomain with '.', since a large domain contains several subdomainsletsubdomains=domains.split('.');while(subdomains.length){//4th, we need to check if there are still subdomains, since we are gonna shift them at the end//join subdomains with '.' because we need to try different combinationsletsubdomain=subdomains.join('.');//add subdomain to visitCount object, if there is already a subdomain in there (such as 'com'),//we just go ahead and add its visitCount. if this is a new subdomain, we add 'visit' as its valuevisitCount[subdomain]=visitCount.hasOwnProperty(subdomain) ?
+(visitCount[subdomain])++(visit) : visit;//+ means Number, otherwise '1' + '0' will become '10' instead of '1'subdomains.shift();}}// visitCount[key] is the value (number), key is the one on the front, which is subdomains.returnObject.keys(visitCount).map((key)=>`${visitCount[key]}${key}`);}