-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0811-subdomain-visit-count.js
More file actions
54 lines (48 loc) · 1.51 KB
/
0811-subdomain-visit-count.js
File metadata and controls
54 lines (48 loc) · 1.51 KB
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
/**
* Subdomain Visit Count
* Time Complexity: O(N * L^2)
* Space Complexity: O(N * L^2)
*/
var subdomainVisits = function (cpdomains) {
const domainCounterStorage = new Map();
for (
let currentDomainEntryIndex = 0;
currentDomainEntryIndex < cpdomains.length;
currentDomainEntryIndex++
) {
const currentDomainInput = cpdomains[currentDomainEntryIndex];
const firstSpacePosition = currentDomainInput.indexOf(" ");
const visitNumberString = currentDomainInput.substring(
0,
firstSpacePosition,
);
const actualDomainName = currentDomainInput.substring(
firstSpacePosition + 1,
);
const parsedVisitNumber = parseInt(visitNumberString);
let nextDotSearchIndex = 0;
while (nextDotSearchIndex !== -1) {
const currentSubdomainKey =
actualDomainName.substring(nextDotSearchIndex);
const existingVisitsCount =
domainCounterStorage.get(currentSubdomainKey) || 0;
domainCounterStorage.set(
currentSubdomainKey,
existingVisitsCount + parsedVisitNumber,
);
nextDotSearchIndex = actualDomainName.indexOf(".", nextDotSearchIndex);
if (nextDotSearchIndex !== -1) {
nextDotSearchIndex++;
}
}
}
const finalOutputList = [];
for (const [
processedDomainKey,
totalAccumulatedVisits,
] of domainCounterStorage.entries()) {
const resultStringElement = `${totalAccumulatedVisits} ${processedDomainKey}`;
finalOutputList.push(resultStringElement);
}
return finalOutputList;
};