|
1 | | -package io.github.dunwu.ds.hashtable; |
| 1 | +package io.github.dunwu.algorithm.hashtable; |
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
4 | 4 | import java.util.HashMap; |
|
47 | 47 | */ |
48 | 48 | public class SubdomainVisitCount { |
49 | 49 |
|
50 | | - public static void main(String[] args) { |
51 | | - SubdomainVisitCount tmpl = new SubdomainVisitCount(); |
52 | | - |
53 | | - String[] s1 = new String[] {"9001 discuss.leetcode.com"} |
54 | | - String[] s2 = new String[] {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"} |
55 | | - tmpl.subdomainVisits(s1); |
56 | | - tmpl.subdomainVisits(s2); |
57 | | - } |
58 | | - |
59 | | - public List<String> subdomainVisits(String[] cpdomains) { |
60 | | - List<String> result = new ArrayList<>(); |
61 | | - Map<String, Integer> map = new HashMap<>(); // key: subdomain, value: frequency |
62 | | - StringBuilder resultStringBuilder = new StringBuilder(); |
63 | | - |
64 | | - for (String cpdomain : cpdomains) { |
65 | | - int indexSpace = cpdomain.indexOf(' '); |
66 | | - int numClicks = Integer.parseInt(cpdomain.substring(0, indexSpace)); |
67 | | - String domain = cpdomain.substring(indexSpace + 1); |
68 | | - resultStringBuilder.setLength(0); |
69 | | - resultStringBuilder.append(domain); |
70 | | - while (true) { |
71 | | - map.put(resultStringBuilder.toString(), |
72 | | - map.getOrDefault(resultStringBuilder.toString(), 0) + numClicks); |
73 | | - int dotPosition = resultStringBuilder.indexOf("."); |
74 | | - if (dotPosition == -1) { break; } |
75 | | - resultStringBuilder.delete(0, dotPosition + 1); |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - for (String domain : map.keySet()) |
80 | | - result.add(map.get(domain) + " " + domain); |
81 | | - |
82 | | - return result; |
83 | | - } |
| 50 | + public static void main(String[] args) { |
| 51 | + SubdomainVisitCount tmpl = new SubdomainVisitCount(); |
| 52 | + |
| 53 | + String[] s1 = new String[] { "9001 discuss.leetcode.com" }; |
| 54 | + String[] s2 = new String[] { "900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org" }; |
| 55 | + tmpl.subdomainVisits(s1); |
| 56 | + tmpl.subdomainVisits(s2); |
| 57 | + } |
| 58 | + |
| 59 | + public List<String> subdomainVisits(String[] cpdomains) { |
| 60 | + List<String> result = new ArrayList<>(); |
| 61 | + Map<String, Integer> map = new HashMap<>(); // key: subdomain, value: frequency |
| 62 | + StringBuilder resultStringBuilder = new StringBuilder(); |
| 63 | + |
| 64 | + for (String cpdomain : cpdomains) { |
| 65 | + int indexSpace = cpdomain.indexOf(' '); |
| 66 | + int numClicks = Integer.parseInt(cpdomain.substring(0, indexSpace)); |
| 67 | + String domain = cpdomain.substring(indexSpace + 1); |
| 68 | + resultStringBuilder.setLength(0); |
| 69 | + resultStringBuilder.append(domain); |
| 70 | + while (true) { |
| 71 | + map.put(resultStringBuilder.toString(), |
| 72 | + map.getOrDefault(resultStringBuilder.toString(), 0) + numClicks); |
| 73 | + int dotPosition = resultStringBuilder.indexOf("."); |
| 74 | + if (dotPosition == -1) { break; } |
| 75 | + resultStringBuilder.delete(0, dotPosition + 1); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + for (String domain : map.keySet()) |
| 80 | + result.add(map.get(domain) + " " + domain); |
| 81 | + |
| 82 | + return result; |
| 83 | + } |
84 | 84 |
|
85 | 85 | } |
0 commit comments