Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d76e889

Browse files
committed
811_Subdomain_Visit_Count
1 parent 1c395a2 commit d76e889

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Remember solutions are only solutions to given problems. If you want full study
150150
| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/743_Network_Delay_Time.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/743_Network_Delay_Time.java) | Let V == N, then: 1. DFS, O(V^V+ElgE), O(V+E)<br>2. Dijkstra, O(V^2+E), O(V+E)|
151151
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | Count given char in string. Hash or table. [Oneline](https://leetcode.com/problems/jewels-and-stones/discuss/113574/1-liners-PythonJavaRuby) |
152152
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/804_Unique_Morse_Code_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/804_Unique_Morse_Code_Words.java) | String, Hash and Set. Set is recommended. |
153+
| 811 | [Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/811_Subdomain_Visit_Count.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/811_Subdomain_Visit_Count.java) | String split and HashMap, O(n) and O(n) |
153154
| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/819_Most_Common_Word.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/819_Most_Common_Word.java) | String processing, be careful about 'b,b,b'. regex is recommended. |
154155
| 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/844_Backspace_String_Compare.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/844_Backspace_String_Compare.java) | 1. Stack pop when encounters #, O(n) and O(n)<br>2. Compare string from end to start, O(n) and O(1) |
155156
| 904 | [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/904_Fruit_Into_Baskets.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/904_Fruit_Into_Baskets.java) | 1. Scan through blocks of tree, O(n) and O(n)<br>2. Mainten a sliding window with start and curr point, O(n) and O(n). |

java/811_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+
// https://leetcode.com/problems/subdomain-visit-count/discuss/121738/C%2B%2BJavaPython-Easy-Understood-Solution
4+
Map<String, Integer> map = new HashMap();
5+
for (String cpdomain : cpdomains) {
6+
int i = cpdomain.indexOf(' ');
7+
int n = Integer.valueOf(cpdomain.substring(0, i));
8+
String domain = cpdomain.substring(i + 1);
9+
for (i = 0; i < domain.length(); ++i) {
10+
if (domain.charAt(i) == '.') {
11+
String d = domain.substring(i + 1);
12+
map.put(d, map.getOrDefault(d, 0) + n);
13+
}
14+
}
15+
map.put(domain, map.getOrDefault(domain, 0) + n);
16+
}
17+
18+
List<String> res = new ArrayList();
19+
for (String domain : map.keySet()) res.add(map.get(domain) + " " + domain);
20+
return res;
21+
}
22+
}

python/811_Subdomain_Visit_Count.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def subdomainVisits(self, cpdomains):
3+
"""
4+
:type cpdomains: List[str]
5+
:rtype: List[str]
6+
"""
7+
domain_count = {}
8+
for cpdomain in cpdomains:
9+
count, domain = cpdomain.split(' ')
10+
sub_domain = domain.split('.')
11+
for i in range(len(sub_domain)):
12+
curr = '.'.join(sub_domain[i:])
13+
domain_count[curr] = domain_count.get(curr, 0) + int(count)
14+
return [str(v) + ' ' + k for k, v in domain_count.items()]
15+
16+
17+
# def subdomainVisits(self, cpdomains):
18+
# # https://leetcode.com/problems/subdomain-visit-count/discuss/121770/Python-short-and-understandable-solution-68-ms
19+
# counter = collections.Counter()
20+
# for cpdomain in cpdomains:
21+
# count, *domains = cpdomain.replace(" ",".").split(".")
22+
# for i in range(len(domains)):
23+
# counter[".".join(domains[i:])] += int(count)
24+
# return [" ".join((str(v), k)) for k, v in counter.items()]

0 commit comments

Comments
 (0)