Problem
Note: This is a companion problem to the System Design problem: Design 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 to the original URL.
Implement the Solution
class:
Solution()
Initializes the object of the system.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 givenshortUrl
was encoded by the same object.
Examples
Example 1:
|
|
Solution
Method 1 - Using Linked List
We can store the urls in the ArrayList
, and return the index at which it is stored.
Code
|
|
Complexity
- ⏰ Time complexity:
O(1)
for both encoding and decoding. - 🧺 Space complexity:
O(n*l)
forn
urls with average lengthl
.
Method 2 - Using Hashmap
What if we want fixed length url, then this method can help. So, we do following
- Use
index
to map generated short url to long url - Use
revIndex
to map long url to short url (We can also use set here, but can help if we need to extend the problem) - We use do while loop to generate 6 digit random string, so that we enter the loop at-least once. We will just generate the short url or key. If it is already present in
index
, we will try again, till we find a new key which is not yet present inindex
. This avoids collisions. - Now, we are out loop, we save this generated
key
orshortUrl
toindex
and reverse mapping torevIndex
.
Code
|
|