AlgoMaster Logo
AlgoMasterResolve DNS CNAME Chainseasy

Resolve DNS CNAME Chains

easy

A DNS CNAME record makes one domain name an alias for another. A resolver may need to follow several CNAME records before it reaches the canonical name with no further alias.

Design a DnsCnameResolver class:

  • DnsCnameResolver() creates a stateless resolver.
  • String resolve(String query, String[] aliasFrom, String[] aliasTo) returns the canonical name reached from query.

The arrays describe directed records: aliasFrom[i] is an alias for aliasTo[i]. Names in aliasFrom are unique. Follow the chain until the current name does not appear in aliasFrom, then return it.

The lookup is case-sensitive, the chain is acyclic, and each call is independent.

Example 1:

Input:

Output:

Explanation: www.example.com aliases to example.com, which aliases to server1.host.net. The last name has no CNAME record.

Example 2:

Input:

Output:

Explanation: x does not appear in aliasFrom, so it is already the canonical result.

Constraints

  • 0 <= aliasFrom.length == aliasTo.length <= 10^5
  • 1 <= query.length <= 255
  • 1 <= aliasFrom[i].length, aliasTo[i].length <= 255
  • Names in aliasFrom are unique.
  • The CNAME graph is acyclic.
  • Comparisons are case-sensitive.
  • At most 100 calls are made to resolve.
Hints

Loading...
CallReturns
new DnsCnameResolver()null
resolve("www.example.com", ["www.example.com","example.com"], ["example.com","server1.host.net"])"server1.host.net"

www.example.com points to example.com, which points to server1.host.net. The final name has no CNAME record.

Run checks these cases. Submit also runs a larger hidden set.