-
Notifications
You must be signed in to change notification settings - Fork 5
/
SubdomainVisitCount.java
46 lines (45 loc) · 1.7 KB
/
SubdomainVisitCount.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*https://leetcode.com/problems/subdomain-visit-count/*/
class Solution {
public List<String> subdomainVisits(String[] cpdomains) {
List<String> subdomainVisits = new ArrayList<String>();
Map<String,Integer> subdomains = new HashMap<String,Integer>();
for (String cpdomain : cpdomains)
{
StringBuilder subdomain = new StringBuilder("");
int count = 0, dig = 0, len = cpdomain.length();
while (dig < len)
{
char ch = cpdomain.charAt(dig);
if (ch >= '0' && ch <= '9')
count = (count*10)+(ch-'0');
else break;
++dig;
}
int chr = len-1;
while (chr != dig)
{
char ch = cpdomain.charAt(chr);
if (ch == '.')
{
String stringSubdomain = subdomain.toString();
subdomains.put(stringSubdomain,subdomains.getOrDefault(stringSubdomain,0)+count);
}
subdomain.append(ch);
--chr;
}
String stringSubdomain = subdomain.toString();
subdomains.put(stringSubdomain,subdomains.getOrDefault(stringSubdomain,0)+count);
}
// System.out.println(subdomains);
for (Map.Entry elem : subdomains.entrySet())
{
StringBuilder build = new StringBuilder("");
build.append((String)elem.getKey());
build.reverse();
build.insert(0," ");
build.insert(0,Integer.toString((Integer)elem.getValue()));
subdomainVisits.add(build.toString());
}
return subdomainVisits;
}
}