Encode and Decode TinyURL

Medium Topics Company Tags

Implement Solution to encode URLs and recover their original values.

  • encode(longUrl) returns an encoded URL or identifier for longUrl.
  • decode(shortUrl) returns the original URL associated with that encoded value. The value passed to decode was produced by the same object.

You may choose any encoding scheme. The judge compares decoded URLs with the originals, so your encoded strings do not need to match a particular format. Your object must preserve all mappings it has created.

Each test case supplies one url and displays its decoded value as the output. The judge may encode several test-case URLs with the same object before decoding them, including decoding them in a different order. Repeated input URLs are allowed.

Example 1:

Input: url = "https://example.com/docs"

Output: "https://example.com/docs"

Explanation: Encoding creates an identifier for this URL. Decoding that identifier with the same object must return the original docs URL exactly.

Example 2:

Input: url = "https://example.com/search?q=graphs&lang=en#results"

Output: "https://example.com/search?q=graphs&lang=en#results"

Explanation: The query parameters and fragment are part of the original URL. They must all be preserved when its encoded value is decoded.

Example 3:

Input: url = "https://example.com/users/7"

Output: "https://example.com/users/7"

Explanation: The encoded value may use any scheme. Decoding it must return this URL even if the object has encoded other URLs in the meantime.

Constraints:

  • 1 <= url.length <= 10000
  • Each input is a valid URL.
  • Every value passed to decode was returned by encode on the same object.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.