Ad platform~15 min2s · 256 MBtarget O(total length) time, O(total length) spacehashing · strings
An ad network logs the page address where each ad impression was shown. A campaign manager wants to know on how many different publisher sites the campaign appeared. Two placement addresses belong to the same publisher site when they have the same hostname after tidying.
An address has up to four parts. An optional scheme comes first, either http:// or https://. Then comes the hostname, then an optional port written as a colon and digits. An optional path follows, starting with /, ? or #.
The ad network tidies the hostname in three ways. Capital letters become lower case. One final dot is removed. A leading www. is removed when something follows it.
Write count_hostnames(urls).
/, ?, # or :.www. when at least one more character follows it.Input: urls = ["https://News.Example.com/a?x=1", "http://example.com:8080/b", "https://www.example.com", "shop.example.com/cart#top", "SHOP.EXAMPLE.COM./", "http://blog.example.org"]
Output: 4| Address | Hostname picked | After tidying |
|---|---|---|
https://News.Example.com/a?x=1 |
News.Example.com |
news.example.com |
http://example.com:8080/b |
example.com |
example.com |
https://www.example.com |
www.example.com |
example.com |
shop.example.com/cart#top |
shop.example.com |
shop.example.com |
SHOP.EXAMPLE.COM./ |
SHOP.EXAMPLE.COM. |
shop.example.com |
http://blog.example.org |
blog.example.org |
blog.example.org |
The six placements give four different publisher sites. example.com and shop.example.com each appear twice.
Input: urls = ["a.com", "A.COM", "a.com/x"]
Output: 1Capital letters and a path do not make a new publisher site.
Input: urls = ["www.a.com", "a.com.", "b.a.com"]
Output: 2www.a.com and a.com. both tidy to a.com. The subdomain b.a.com is a different publisher site.
1 ≤ length of urls ≤ 10^51 ≤ length of urls[i] ≤ 100.:/?#_=&- onlyIf every new hostname is compared with a list of all the earlier ones, an impression log with 50,000 different publisher sites makes more than a billion comparisons.
A set looks a name up in one step. Each address then costs only the time to read its own characters, at most 100.