-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL811.java
More file actions
82 lines (74 loc) · 3.21 KB
/
L811.java
File metadata and controls
82 lines (74 loc) · 3.21 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
class Solution811 {
class Solution {
/**
* 811. Subdomain Visit Count https://leetcode.com/problems/subdomain-visit-count/description/
*
* @param cpdomains String[]
* Array of counts and domains.
* @return List of domains and their visit counts
* @timeComplexity O(n * k) where n is the number of domains and k is the depth of a domain at any level
* @spaceComplexity O(n) space for building tree. n is the number of domains.
*/
public List<String> subdomainVisits(String[] cpdomains) {
Node root = new Node();
// Add all the domains to a tree and update the hitcounts
for (String domain : cpdomains) {
String[] countSplit = domain.split(" ");
String[] domainParts = countSplit[1].split("\\.");
addDomain(root, domainParts, domainParts.length - 1, Integer.parseInt(countSplit[0]));
}
// Enumerate all paths in the tree from root to leaf (including internal nodes)
List<String> resultList = new ArrayList<>();
traverseTree(root, resultList, "", 0);
return resultList;
}
// Structure for tree
class Node {
int count;
String domain;
List<Node> children;
public Node(String domain, int count) {
this.domain = domain;
this.count = count;
this.children = new ArrayList<>();
}
public Node() {
// Root node has blank domain and count as -1 as demarker
this("", -1);
}
}
// Traverse the tree finding all the counts and updating result list
private void traverseTree(Node root, List<String> resultList, String currentDomain, int currentCount) {
if (root != null) {
String updatedDomain = "";
if (root.count != -1) {
updatedDomain = root.domain + (currentDomain.length() == 0 ? "" : "." + currentDomain);
resultList.add(currentCount + root.count + " " + updatedDomain);
}
for (Node child : root.children) {
traverseTree(child, resultList, updatedDomain, currentCount);
}
}
}
// Add a domain to the tree
private void addDomain(Node root, String[] domainParts, int index, int hitCount) {
if (index >= 0) {
String currentDomain = domainParts[index];
// Find the current node
Node child;
Optional<Node> childOpt = root.children.stream().filter(n -> n.domain.equalsIgnoreCase(currentDomain)).findFirst();
if (childOpt.isPresent()) {
child = childOpt.get();
child.count += hitCount;
} else {
child = new Node(currentDomain, hitCount);
root.children.add(child);
}
addDomain(child, domainParts, index - 1, hitCount);
}
}
}
}