Your task is to design a service that can encode a URL into a shortened URL and decode the shortened URL to its original form. This is similar to the functionality provided by services like TinyURL.
encode(longUrl: str) -> str
: Encodes a long URL to a shortened URL.decode(shortUrl: str) -> str
: Decodes a shortened URL to its original URL.There’s no limit to the number of URLs that can be encoded, so assume infinite URLs can be handled.
# Example usage
url_service = Codec()
short_url = url_service.encode("https://leetcode.com/problems/design-tinyurl")
# returns "http://tinyurl.com/abc123"
assert url_service.decode(short_url) == "https://leetcode.com/problems/design-tinyurl"
Assuming the answers are:
encode
and decode
methods accordingly.url_map
to store mappings for short URL to long URL.import string
import random
import hashlib
class Codec:
def __init__(self):
self.url_map = {}
self.counter = 0
self.base_url = "http://tinyurl.com/"
def _generate_hash(self, longUrl: str) -> str:
"""
Generate a unique hash for the given URL using a combination of a hash function
and internal counter to avoid collisions.
"""
self.counter += 1
hash_object = hashlib.md5(f"{longUrl}{self.counter}".encode())
short_hash = hash_object.hexdigest()[:6]
return short_hash
def encode(self, longUrl: str) -> str:
"Encodes a URL to a shortened URL."
# Generate short hash
short_hash = self._generate_hash(longUrl)
# Handle potential collisions
while short_hash in self.url_map:
short_hash = self._generate_hash(longUrl)
# Store in url_map
short_url = self.base_url + short_hash
self.url_map[short_url] = longUrl
return short_url
def decode(self, shortUrl: str) -> str:
"Decodes a shortened URL to its original URL."
return self.url_map.get(shortUrl, "")
# Example usage
codec = Codec()
long_url = "https://leetcode.com/problems/design-tinyurl"
short_url = codec.encode(long_url)
print("Short URL:", short_url)
assert codec.decode(short_url) == long_url
print("Decoded URL:", codec.decode(short_url))
Codec
initializes the URL mapping dictionary and the base URL._generate_hash
method uses the MD5 hash function to create a unique hash of the URL concatenated with a counter, ensuring uniqueness.This design ensures that the encoder and decoder are both efficient and handle potential hash collisions.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?