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^51 <= query.length <= 2551 <= 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.