/*
* @lc app=leetcode.cn id=811 lang=typescript
*
* [811] 子域名访问计数
*/
// @lc code=start
function subdomainVisits(cpdomains: string[]): string[] {}
// @lc code=end
function subdomainVisits(cpdomains: string[]): string[] {
const res = new Map<string, number>()
for (let str of cpdomains) {
const strs = str.split(' ')
const num = Number(strs[0]),
names = strs[1].split('.')
let s = ''
for (let i = names.length - 1; i >= 0; i--) {
if (i < names.length - 1) s = '.' + s
s = names[i] + s
res.set(s, (res.get(s) ?? 0) + num)
}
}
return [...res].map(arr => `${arr[1]} ${arr[0]}`)
}
test.each([
{
input: { cpdomains: ['9001 discuss.leetcode.com'] },
output: ['9001 leetcode.com', '9001 discuss.leetcode.com', '9001 com'],
},
{
input: { cpdomains: ['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',
],
},
])('input: cpdomains = $input.cpdomains', ({ input: { cpdomains }, output }) => {
expect(subdomainVisits(cpdomains)).toIncludeSameMembers(output)
})