DSA problem
Encode and Decode TinyURL
TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk. Design a class to encode a URL and decode a tiny URL.
There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded back to the original URL.
Implement the Codec class:
Codec()initializes the object.String encode(String longUrl)returns a tiny URL for the givenlongUrl.String decode(String shortUrl)returns the original long URL for the givenshortUrl. It is guaranteed that the givenshortUrlwas encoded by the same object.
Examples
Example 1
Input:
url = "https://leetcode.com/problems/design-tinyurl"Output:
"https://leetcode.com/problems/design-tinyurl"Explanation: Codec codec = new Codec(); String tiny = codec.encode(url); String original = codec.decode(tiny); original equals the input url. encode produces something like "http://tinyurl.com/0", and decode maps it back to the original long URL.
Constraints
- 1 <= url.length <= 10^4
- url is guaranteed to be a valid URL.
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.