Medium

Quiz

#535 Encode and Decode TinyURL

APPROACH

Design a URL shortening system. Your implementation must be able to shorten any long URL into a compact short URL and reliably restore the original URL from the shortened form.

You have complete freedom in choosing your encoding scheme — the only requirement is that encoding then decoding always recovers the original URL.

Implement the Solution class:

- Solution() sets up the system.

- String encode(String longUrl) returns a shortened URL for longUrl.

- String decode(String shortUrl) returns the original URL corresponding to shortUrl (guaranteed to have been produced by encode).

Example 1:

Input: url = "https://leetcode.com/problems/design-tinyurl"
Output: "https://leetcode.com/problems/design-tinyurl"

Example 2:

Explanation:
Solution obj = new Solution();
string tiny = obj.encode(url); // returns the encoded tiny url.
string ans = obj.decode(tiny); // returns the original url after decoding it.
1 of 4
1:00

What is the optimal approach for this problem?