#C12254. Group URLs by Domain
Group URLs by Domain
Group URLs by Domain
You are given a list of URLs. Your task is to group these URLs based on their domain names. For each URL, extract its domain by parsing the URL. If the URL does not contain a valid domain (for example, when no protocol is provided), assign it to the domain unknown.
The domain of a URL is defined as follows:
If the URL starts with a scheme such as http://, https:// or ftp://, then the domain is the substring immediately following the scheme up to the next '/' character (or to the end of the string if '/' is not present). If the URL does not include one of these schemes, then consider its domain as unknown.
For example, consider the URL https://www.example.com/path/to/page. Its domain is www.example.com. On the other hand, for the URL www.server.com/path/to/resource, since it lacks a protocol, the domain is unknown.
inputFormat
The input is read from standard input (stdin) and has the following format:
N URL_1 URL_2 ... URL_N
Where N is the number of URLs. Each of the following N lines contains a URL as a string.
outputFormat
Output to standard output (stdout) a JSON object (dictionary) where the keys are domain names and the values are lists (arrays) of URLs corresponding to that domain.
If a URL does not have a valid domain (see definition above), it should be grouped under the key "unknown"
.
Note: The output must be a valid JSON string. The order of keys in the JSON object does not matter.
## sample6
https://www.example.com/path/to/page
http://example.com/another/path
https://www.google.com/
ftp://files.server.com/download
www.server.com/path/to/resource
https://example.com/yet/another/path
{"www.example.com": ["https://www.example.com/path/to/page"], "example.com": ["http://example.com/another/path", "https://example.com/yet/another/path"], "www.google.com": ["https://www.google.com/"], "files.server.com": ["ftp://files.server.com/download"], "unknown": ["www.server.com/path/to/resource"]}