SDE Path

URL Shortener

Easy

URL Shortener

TinyURL's core: mint a short code for every distinct URL, hand back the same code on repeats, and resolve codes back to the original link.

Requirements

  • Deterministic, dedup-first codes. A monotonic counter starts at 0. The first distinct URL gets code 0, the next 1, and so on. Shortening an already-seen URL returns the same code and does not advance the counter.
  • Code alphabet is base-62 in this exact order: digits 0-9, then lowercase a-z, then uppercase A-Z (so counter 0→0, 9→9, 10→a, 35→z, 36→A, 61→Z, 62→10). The code is the standard positional base-62 encoding of the counter, most-significant digit first, with no leading padding.
  • resolve on a code that was never issued returns the literal token null.
  • URLs are opaque, whitespace-free tokens; two URLs are "the same" iff their strings are byte-for-byte equal.

API to implement

Implement the class UrlShortener. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.

| Command | Returns | Behavior | |---|---|---| | shorten <url> | string | Return a short code for url. Codes are assigned deterministically: if this exact URL was shortened before, return its existing code; otherwise assign the base-62 encoding of the next counter value (starting at 0) and advance the counter. | | resolve <code> | string | Return the original URL for code, or the literal null if no such code has been issued. |

Input format

The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.

Output format

For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.

This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.

Sample cases

Example 1
Input
6
shorten https://a.com
shorten https://b.com
shorten https://a.com
resolve 0
resolve 1
resolve 2
Expected output
0
1
0
https://a.com
https://b.com
null
Example 2
Input
5
shorten x.io/p
resolve z
shorten y.io/q
shorten x.io/p
resolve 1
Expected output
0
null
1
0
y.io/q