일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Kaggle
- ubuntu
- 코로나19
- 자연어처리
- 프로그래머스
- Docker
- 캐치카페
- 데이콘
- AI 경진대회
- 더현대서울 맛집
- github
- hackerrank
- 프로그래머스 파이썬
- Git
- Real or Not? NLP with Disaster Tweets
- 편스토랑 우승상품
- 우분투
- 백준
- gs25
- programmers
- SW Expert Academy
- PYTHON
- dacon
- Baekjoon
- 파이썬
- 편스토랑
- leetcode
- 금융문자분석경진대회
- ChatGPT
- 맥북
- Today
- Total
솜씨좋은장씨
[leetCode] 811. Subdomain Visit Count (Python) 본문
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.
Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".
We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.
Example 1:
Input:
["9001 discuss.leetcode.com"]
Output:
["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
Explanation:
We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain
"leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.
Example 2:
Input:
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
Output:
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
Explanation:
We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org"
5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951
times, and "org" 5 times.
Notes:
- The length of cpdomains will not exceed 100.
- The length of each domain name will not exceed 100.
- Each address will have either 1 or 2 "." characters.
- The input count in any count-paired domain will not exceed 10000.
- The answer output can be returned in any order.
Solution
class Solution:
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
domain_dict={}
for domain in cpdomains:
domain=domain.replace(' ','.').split('.')
for i in range(1,len(domain)):
sub_domain = '.'.join(domain[i:])
domain_dict[sub_domain] = domain_dict.get(sub_domain,0)+int(domain[0])
answer = [' '.join([str(val),key]) for key ,val in domain_dict.items()]
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 1365. How Many Numbers Are Smaller Than the Current Number (Python) (2) | 2020.09.14 |
---|---|
[leetCode] 500. Keyboard Row (Python) (0) | 2020.09.13 |
[leetCode] 1002. Find Common Characters (Python) (0) | 2020.09.12 |
[leetCode] 28. Implement strStr() (Python) (0) | 2020.09.10 |
[leetCode] 1337. The K Weakest Rows in a Matrix (Python) (0) | 2020.09.09 |