Skip to content

Commit e80d7a0

Browse files
authored
Update and rename Easy/Subdomain Visit Count.java to Medium/Subdomain Visit Count.java
1 parent 9d1ac1c commit e80d7a0

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

Easy/Subdomain Visit Count.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

Medium/Subdomain Visit Count.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public List<String> subdomainVisits(String[] cpdomains) {
3+
Map<String, Long> map = new HashMap<>();
4+
for (String cpdomain : cpdomains) {
5+
int spaceIdx = cpdomain.indexOf(' ');
6+
int count = Integer.parseInt(cpdomain.substring(0, spaceIdx));
7+
String[] subdomains = cpdomain.substring(spaceIdx + 1).split("\\.");
8+
StringBuilder sb = new StringBuilder();
9+
for (int i = subdomains.length - 1; i >= 0; i--) {
10+
sb.insert(0, subdomains[i]);
11+
String currDomain = sb.toString();
12+
map.put(currDomain, map.getOrDefault(currDomain, 0L) + count);
13+
sb.insert(0, ".");
14+
}
15+
}
16+
List<String> result = new ArrayList<>();
17+
for (String key : map.keySet()) {
18+
result.add(map.get(key) + " " + key);
19+
}
20+
return result;
21+
}
22+
}

0 commit comments

Comments
 (0)