#535
Encode and Decode TinyURL
pupil · 455 · lc medium +26 · verified · 86.6% accepted · 2,132 likes · top 98%
Description
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.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15