[알고리즘] Subdomain Visit Count

Jerry·2021년 1월 21일
0

LeetCode

목록 보기
8/73
post-thumbnail

LeetCode - Subdomain Visit Count

문제 설명

A website domain like "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.

Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".

We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.

Example 1

Input:
["9001 discuss.leetcode.com"]
Output:
["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
Explanation:
We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.

Example 2

Input:
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
Output:
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
Explanation:
We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.

Note

  • The length of cpdomains will not exceed 100.
  • The length of each domain name will not exceed 100.
  • Each address will have either 1 or 2 "." characters.
  • The input count in any count-paired domain will not exceed 10000.
  • The answer output can be returned in any order.

Solution

/**
 * @param {string[]} cpdomains
 * @return {string[]}
 */
var subdomainVisits = function (cpdomains) {
    
    let map = new Map();
    for (let cpdomain of cpdomains) {
        let starIdx = 0;
        let splitIdx = 0;
        let count = 0;
        // 제일 앞의 숫자 구분을 위한 구분값
        splitIdx = cpdomain.indexOf(' ');
        //substring(start,end) =>  [start:end-1]
        count = cpdomain.substring(starIdx, splitIdx);

        starIdx = splitIdx + 1;
        // endIdx = cpdomain.lastIndexOf('.', starIdx); 
        for (; splitIdx != -1;) {

            // substring의 종료 인덱스가 음수인 경우, 암시적을 마지막 인덱스 할당
            cpdomain = cpdomain.substring(starIdx)
            starIdx = 0;
            
            let preCount = map.get(cpdomain);
            if (preCount == undefined) { // map.get 에서 요소를 찾기 못하는 경우 undefined 반환.
                preCount = 0;
            }
            map.set(cpdomain, Number(count) + preCount);
            splitIdx = cpdomain.indexOf('.', starIdx);

            starIdx = splitIdx + 1;
        }
    }

    let res = [];
    for (let pair of map) {
      //  console.log(pair[1] + ' ' + pair[0]); // pair[0] : key, pair[1]: value
        res.push(pair[1] + ' ' + pair[0]);
    }
    return res;
};

새로 알게된 내용

  • null 과 undefined 차이
    - 둘다 값이 없음을 나타내는 것은 같음.
    - 동등연산자(==)로는 두개 비교시 true 리턴.
    - 형타입까지 비교하는 (===) 연산자로는 false 리턴.

~.~

profile
제리하이웨이

0개의 댓글