495. Check Whether URLs Are Linked Through Referrer Logs
Asked in
Check Whether URLs Are Linked Through Referrer Logs

A website stores every page visit in its logs. Each log entry has the visited url and its referrer, which is the page the visitor came from. A referrer of "-" means the page was opened directly, without a referrer.

Every log entry with a referrer creates a one-way link from referrer to url. URL a is linked to URL b if b can be reached from a by following one or more of these links.

Implement the UrlLinkChecker class to check whether two URLs are linked through the referrer logs.

Methods

Constructor

UrlLinkChecker(List<String> logs)
  • Each entry of logs is in the format "url,referrer".
  • logs may contain repeated entries, self links and cycles.

Check Link

boolean areURLsLinked(String a, String b)
  • Returns true if b can be reached from a by following one or more links, otherwise returns false.
  • Links are one-way: a link from /x to /y does not link /y to /x.
  • A URL that does not appear in logs has no links.

Constraints

  • 1 ≤ logs.size() ≤ 10,000
  • Each entry of logs is in the format "url,referrer".
  • 1 ≤ length of every URL ≤ 50
  • Every URL starts with '/' and contains only lowercase English letters, digits, '/', '.' and '-'.
  • referrer is either "-" or a URL in the same format.
  • a and b are URLs in the same format and a ≠ b.
  • At most 1,000 calls will be made to areURLsLinked.

Examples

Example 1

UrlLinkChecker checker = new UrlLinkChecker(logs = ["/home,-", "/menu,/home", "/menu,/home", "/order,/menu", "/home,/order", "/faq,-", "/contact,/faq"])

Links created: /home → /menu, /menu → /order, /order → /home, /faq → /contact. The repeated entry creates the same link again, and entries with referrer "-" create no link.

  • checker.areURLsLinked(a = "/home", b = "/order") returns true, path: /home → /menu → /order.
  • checker.areURLsLinked(a = "/order", b = "/menu") returns true, path: /order → /home → /menu.
  • checker.areURLsLinked(a = "/contact", b = "/faq") returns false, links are one-way and /contact has no outgoing link.
  • checker.areURLsLinked(a = "/menu", b = "/faq") returns false, starting from /menu only /order, /home and /menu can be reached.

Example 2

UrlLinkChecker checker = new UrlLinkChecker(logs = ["/blog,/blog", "/blog/post-1,/blog", "/blog/post-2,/blog/post-1", "/rss.xml,-"])

Links created: /blog → /blog, /blog → /blog/post-1, /blog/post-1 → /blog/post-2.

  • checker.areURLsLinked(a = "/blog", b = "/blog/post-2") returns true, path: /blog → /blog/post-1 → /blog/post-2.
  • checker.areURLsLinked(a = "/blog/post-2", b = "/blog") returns false, /blog/post-2 has no outgoing link.
  • checker.areURLsLinked(a = "/blog", b = "/rss.xml") returns false, /rss.xml was only opened directly, so no link leads to it.
  • checker.areURLsLinked(a = "/login", b = "/blog") returns false, /login does not appear in logs.


Please use Laptop/Desktop or any other large screen to add/edit code.