LeetCode 题解工作台

子域名访问计数

网站域名 "discuss.leetcode.com" 由多个子域名组成。顶级域名为 "com" ,二级域名为 "leetcode.com" ,最低一级为 "discuss.leetcode.com" 。当访问域名 "discuss.leetcode.com" 时,同时也会隐式访问其父域名 "lee…

category

4

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们用哈希表 `cnt` 存储每个域名(子域名)对应的访问次数。 然后遍历数组,对于每个域名,我们将其拆分为子域名,然后更新哈希表 `cnt` 中对应的访问次数。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

网站域名 "discuss.leetcode.com" 由多个子域名组成。顶级域名为 "com" ,二级域名为 "leetcode.com" ,最低一级为 "discuss.leetcode.com" 。当访问域名 "discuss.leetcode.com" 时,同时也会隐式访问其父域名 "leetcode.com" 以及 "com"

计数配对域名 是遵循 "rep d1.d2.d3""rep d1.d2" 格式的一个域名表示,其中 rep 表示访问域名的次数,d1.d2.d3 为域名本身。

  • 例如,"9001 discuss.leetcode.com" 就是一个 计数配对域名 ,表示 discuss.leetcode.com 被访问了 9001 次。

给你一个 计数配对域名 组成的数组 cpdomains ,解析得到输入中每个子域名对应的 计数配对域名 ,并以数组形式返回。可以按 任意顺序 返回答案。

 

示例 1:

输入:cpdomains = ["9001 discuss.leetcode.com"]
输出:["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]
解释:例子中仅包含一个网站域名:"discuss.leetcode.com"。
按照前文描述,子域名 "leetcode.com" 和 "com" 都会被访问,所以它们都被访问了 9001 次。

示例 2:

输入:cpdomains = ["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 次。

 

提示:

  • 1 <= cpdomain.length <= 100
  • 1 <= cpdomain[i].length <= 100
  • cpdomain[i] 会遵循 "repi d1i.d2i.d3i""repi d1i.d2i" 格式
  • repi 是范围 [1, 104] 内的一个整数
  • d1id2id3i 由小写英文字母组成
lightbulb

解题思路

方法一:哈希表

我们用哈希表 cnt 存储每个域名(子域名)对应的访问次数。

然后遍历数组,对于每个域名,我们将其拆分为子域名,然后更新哈希表 cnt 中对应的访问次数。

最后,我们将哈希表中的键值对转换为数组,即可得到答案。

时间复杂度 O(L)O(L),空间复杂度 O(L)O(L)。其中 LL 是数组 cpdomains 中所有域名的长度之和。

1
2
3
4
5
6
7
8
9
10
class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        cnt = Counter()
        for s in cpdomains:
            v = int(s[: s.index(' ')])
            for i, c in enumerate(s):
                if c in ' .':
                    cnt[s[i + 1 :]] += v
        return [f'{v} {s}' for s, v in cnt.items()]
speed

复杂度分析

指标
时间O(n \cdot m)
空间O(n \cdot m)
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for efficient handling of both domain parsing and counting subdomains.

  • question_mark

    Ensure the candidate uses an appropriate data structure for counting subdomains (like a hash map).

  • question_mark

    Test the candidate's understanding of subdomain hierarchy and handling nested domains.

warning

常见陷阱

外企场景
  • error

    Failing to account for all subdomains of a given domain, including its parent domains.

  • error

    Incorrectly managing the subdomain hierarchy, leading to missing visit counts for certain subdomains.

  • error

    Not handling cases where multiple domains share the same subdomain.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Consider handling cases with very large input sizes, ensuring the solution scales efficiently.

  • arrow_right_alt

    Test cases with varying domain levels (e.g., some domains with three parts, others with two).

  • arrow_right_alt

    Edge cases where a domain has no subdomains.

help

常见问题

外企场景

子域名访问计数题解:数组·哈希·扫描 | LeetCode #811 中等