0811-子域名访问计数

Raphael Liu Lv10

网站域名 "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 由小写英文字母组成

方法一:哈希表

每个计数配对域名的格式都是 “rep d1.d2.d3” 或 “rep d1.d2”。子域名的计数如下:

  • 对于格式 “rep d1.d2.d3”,有三个子域名 “d1.d2.d3”、”d2.d3” 和 “d3”,每个子域名各被访问 rep 次;

  • 对于格式 “rep d1.d2”,有两个子域名 “d1.d2” 和 “d2”,每个子域名各被访问 rep 次。

为了获得每个子域名的计数配对域名,需要使用哈希表记录每个子域名的计数。遍历数组 cpdomains,对于每个计数配对域名,获得计数和完整域名,更新哈希表中的每个子域名的访问次数。

遍历数组 cpdomains 之后,遍历哈希表,对于哈希表中的每个键值对,关键字是子域名,值是计数,将计数和子域名拼接得到计数配对域名,添加到答案中。

[sol1-Python3]
1
2
3
4
5
6
7
8
9
10
11
class Solution:
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
cnt = Counter()
for domain in cpdomains:
c, s = domain.split()
c = int(c)
cnt[s] += c
while '.' in s:
s = s[s.index('.') + 1:]
cnt[s] += c
return [f"{c} {s}" for s, c in cnt.items()]
[sol1-Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public List<String> subdomainVisits(String[] cpdomains) {
List<String> ans = new ArrayList<String>();
Map<String, Integer> counts = new HashMap<String, Integer>();
for (String cpdomain : cpdomains) {
int space = cpdomain.indexOf(' ');
int count = Integer.parseInt(cpdomain.substring(0, space));
String domain = cpdomain.substring(space + 1);
counts.put(domain, counts.getOrDefault(domain, 0) + count);
for (int i = 0; i < domain.length(); i++) {
if (domain.charAt(i) == '.') {
String subdomain = domain.substring(i + 1);
counts.put(subdomain, counts.getOrDefault(subdomain, 0) + count);
}
}
}
for (Map.Entry<String, Integer> entry : counts.entrySet()) {
String subdomain = entry.getKey();
int count = entry.getValue();
ans.add(count + " " + subdomain);
}
return ans;
}
}
[sol1-C#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Solution {
public IList<string> SubdomainVisits(string[] cpdomains) {
IList<string> ans = new List<string>();
Dictionary<string, int> counts = new Dictionary<string, int>();
foreach (string cpdomain in cpdomains) {
int space = cpdomain.IndexOf(' ');
int count = int.Parse(cpdomain.Substring(0, space));
string domain = cpdomain.Substring(space + 1);
if (!counts.ContainsKey(domain)) {
counts.Add(domain, 0);
}
counts[domain] += count;
for (int i = 0; i < domain.Length; i++) {
if (domain[i] == '.') {
string subdomain = domain.Substring(i + 1);
if (!counts.ContainsKey(subdomain)) {
counts.Add(subdomain, 0);
}
counts[subdomain] += count;
}
}
}
foreach (KeyValuePair<string, int> pair in counts) {
string subdomain = pair.Key;
int count = pair.Value;
ans.Add(count + " " + subdomain);
}
return ans;
}
}
[sol1-C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
vector<string> ans;
unordered_map<string, int> counts;
for (auto &&cpdomain : cpdomains) {
int space = cpdomain.find(' ');
int count = stoi(cpdomain.substr(0, space));
string domain = cpdomain.substr(space + 1);
counts[domain] += count;
for (int i = 0; i < domain.size(); i++) {
if (domain[i] == '.') {
string subdomain = domain.substr(i + 1);
counts[subdomain] += count;
}
}
}
for (auto &&[subdomain, count] : counts) {
ans.emplace_back(to_string(count) + " " + subdomain);
}
return ans;
}
};
[sol1-C]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
typedef struct {
const char *key;
int val;
UT_hash_handle hh;
} HashItem;

HashItem *hashFindItem(HashItem **obj, const char *key) {
HashItem *pEntry = NULL;
HASH_FIND_STR(*obj, key, pEntry);
return pEntry;
}

bool hashAddItem(HashItem **obj, const char* key, int val) {
if (hashFindItem(obj, key)) {
return false;
}
HashItem *pEntry = (HashItem *)malloc(sizeof(HashItem));
pEntry->key = key;
pEntry->val = val;
HASH_ADD_STR(*obj, key, pEntry);
return true;
}

bool hashSetItem(HashItem **obj, const char* key, int val) {
HashItem *pEntry = hashFindItem(obj, key);
if (!pEntry) {
hashAddItem(obj, key, val);
} else {
pEntry->val = val;
}
return true;
}

int hashGetItem(HashItem **obj, char* key, int defaultVal) {
HashItem *pEntry = hashFindItem(obj, key);
if (!pEntry) {
return defaultVal;
}
return pEntry->val;
}

void hashFree(HashItem **obj) {
HashItem *curr = NULL, *tmp = NULL;
HASH_ITER(hh, *obj, curr, tmp) {
HASH_DEL(*obj, curr);
free(curr);
}
}

char ** subdomainVisits(char ** cpdomains, int cpdomainsSize, int* returnSize){
HashItem *counts = NULL;
for (int i = 0; i < cpdomainsSize; i++) {
int space = strchr(cpdomains[i], ' ') - cpdomains[i];
int count = atoi(cpdomains[i]);
char *domain = cpdomains[i] + space + 1;
hashSetItem(&counts, domain, hashGetItem(&counts, domain, 0) + count);
int len = strlen(domain);
for (int j = 0; j < len; j++) {
if (domain[j] == '.') {
char *subdomain = domain + j + 1;
hashSetItem(&counts, subdomain, hashGetItem(&counts, subdomain, 0) + count);
}
}
}
char **ans = (char **)malloc(sizeof(char *) * cpdomainsSize * 4);
int pos = 0;
*returnSize = HASH_COUNT(counts);
for (HashItem *pEntry = counts; pEntry != NULL; pEntry = pEntry->hh.next) {
ans[pos] = (char *)malloc(sizeof(char) * (strlen(pEntry->key) + 32));
sprintf(ans[pos], "%d %s", pEntry->val, pEntry->key);
pos++;
}
hashFree(&counts);
return ans;
}
[sol1-JavaScript]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var subdomainVisits = function(cpdomains) {
const ans = [];
const counts = new Map();
for (const cpdomain of cpdomains) {
const space = cpdomain.indexOf(' ');
const count = parseInt(cpdomain.slice(0, space));
const domain = cpdomain.slice(space + 1);
counts.set(domain, (counts.get(domain) || 0) + count);
for (let i = 0; i < domain.length; i++) {
if (domain[i] === '.') {
const subdomain = domain.slice(i + 1);
counts.set(subdomain, (counts.get(subdomain) || 0) + count);
}
}
}
for (const [subdomain, count] of counts.entries()) {
ans.push(count + " " + subdomain);
}
return ans;
};
[sol1-Golang]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func subdomainVisits(cpdomains []string) []string {
cnt := map[string]int{}
for _, s := range cpdomains {
i := strings.IndexByte(s, ' ')
c, _ := strconv.Atoi(s[:i])
s = s[i+1:]
cnt[s] += c
for {
i := strings.IndexByte(s, '.')
if i < 0 {
break
}
s = s[i+1:]
cnt[s] += c
}
}
ans := make([]string, 0, len(cnt))
for s, c := range cnt {
ans = append(ans, strconv.Itoa(c)+" "+s)
}
return ans
}

复杂度分析

  • 时间复杂度:O(L),其中 L 是数组 cpdomains 中的所有字符串长度之和。遍历数组中所有的计数配对域名计算每个子域名的计数需要 O(L) 的时间,遍历哈希表也需要 O(L) 的时间。

  • 空间复杂度:O(L),其中 L 是数组 cpdomains 中的所有字符串长度之和。哈希表需要 O(L) 的空间。

 Comments
On this page
0811-子域名访问计数