1. 程式人生 > >C#LeetCode刷題之#811-子域名訪問計數​​​​​​​(Subdomain Visit Count)

C#LeetCode刷題之#811-子域名訪問計數​​​​​​​(Subdomain Visit Count)

問題

一個網站域名,如"discuss.leetcode.com",包含了多個子域名。作為頂級域名,常用的有"com",下一級則有"leetcode.com",最低的一級為"discuss.leetcode.com"。當我們訪問域名"discuss.leetcode.com"時,也同時訪問了其父域名"leetcode.com"以及頂級域名 "com"。

給定一個帶訪問次數和域名的組合,要求分別計算每個域名被訪問的次數。其格式為訪問次數+空格+地址,例如:"9001 discuss.leetcode.com"。

接下來會給出一組訪問次數和域名組合的列表cpdomains 。要求解析出所有域名的訪問次數,輸出格式和輸入格式相同,不限定先後順序。

輸入: ["9001 discuss.leetcode.com"]

輸出: ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]

說明: 例子中僅包含一個網站域名:"discuss.leetcode.com"。按照前文假設,子域名"leetcode.com"和"com"都會被訪問,所以它們都被訪問了9001次。

輸入: ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]

輸出: ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]

說明: 按照假設,會訪問"google.mail.com" 900次,"yahoo.com" 50次,"intel.mail.com" 1次,"wiki.org" 5次。而對於父域名,會訪問"mail.com" 900+1 = 901次,"com" 900 + 50 + 1 = 951次,和 "org" 5 次。

注意事項:

cpdomains 的長度小於 100。 每個域名的長度小於100。 每個域名地址包含一個或兩個"."符號。 輸入中任意一個域名的訪問次數都小於10000。

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.

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.

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.

Notes:

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.

示例

public class Program
{

    public static void Main(string[] args)
    {
        var cpdomains = new string[] { "9001 discuss.leetcode.com" };

        var res = SubdomainVisits(cpdomains);
        ShowArray(res);

        Console.ReadKey();
    }

    private static void ShowArray(IList<string> array)
    {
        foreach (var domain in array)
        {
            Console.Write($"{domain} ");
        }
        Console.WriteLine();
    }

    private static IList<string> SubdomainVisits(string[] cpdomains)
    {
        var dic = new Dictionary<string, int>();
        foreach (var domain in cpdomains)
        {
            var split = domain.Split(' ');
            var num = int.Parse(split[0]);
            var subDomains = GetSubDomains2(split[1]);
            foreach (var item in subDomains)
            {
                if (dic.ContainsKey(item))
                {
                    dic[item] += num;
                }
                else
                {
                    dic[item] = num;
                }
            }
        }
        return dic.Select(r => r.Value + " " + r.Key).ToList();
    }

    private static List<string> GetSubDomains(string domain)
    {
        var res = new List<string>();
        var domains = domain.Split('.');
        for (var i = 0; i < domains.Length; i++)
        {
            var dom = string.Empty;
            for (var j = i; j < domains.Length; j++)
            {
                dom += "." + domains[j];
            }
            res.Add(dom.TrimStart('.'));
        }
        return res;
    }

    private static List<string> GetSubDomains2(string domain)
    {
        var res = new List<string>();
        for (var i = domain.Length - 1; i >= 0; i--)
        {
            if (domain[i] == '.' || i == 0)
            {
                res.Add(domain.Substring(i).TrimStart('.'));
            }
        }
        return res;
    }

}

以上給出1種演算法實現,以下是這個案例的輸出結果:

9001 discuss.leetcode.com 9001 leetcode.com 9001 com

分析:

設原問題規模為 n,域名的最大長度為 m,若獲取子域名採用 GetSubDomains 方法,那麼以上演算法的時間複雜度為: O(n*m^{^{2}}) ;

若獲取子域名採用 GetSubDomains2 方法,則以上演算法的時間複雜度為: O(n*m) 。