#K35042. Group URLs by Domain and Subdomain
Group URLs by Domain and Subdomain
Group URLs by Domain and Subdomain
You are given a list of URLs. Your task is to group these URLs by their root domain and subdomain. The root domain is defined as the last two components of the hostname. For example, in mail.google.com
, the root domain is google.com and the subdomain is mail.google.com
.
You should extract the hostname using standard URL parsing techniques. Then, determine the root domain using the formula $$RootDomain = join(lastTwo(split(hostname, '.')))$$ and group the URLs accordingly. The final output must display each root domain and, under it, each subdomain with its corresponding list of URLs. Use 4 spaces for indentations at each level.
inputFormat
The first line contains an integer n
representing the number of URLs. The following n
lines each contain one URL.
outputFormat
Output the grouped URLs in the following format:
root_domain: subdomain: URL URL subdomain: URL
That is, for each root domain, print it followed by a colon. Then for each subdomain under that root domain, print 4 spaces and the subdomain followed by a colon, and for each URL under the subdomain print 8 spaces followed by the URL.
## sample3
https://mail.google.com
http://www.google.com
https://docs.google.com
google.com:
mail.google.com:
https://mail.google.com
www.google.com:
http://www.google.com
docs.google.com:
https://docs.google.com</code></pre>