tinycodinglm / cp /cp-10041.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
3.12 kB
opencode trace
System You are an expert LeetCode problem solver. You solve problems efficiently with clean code.

You are an expert LeetCode problem solver. You solve problems efficiently with clean code.

User

Problem: Encode and Decode TinyURL (Difficulty: Medium)
Tags: Hash Table, String, Design, Hash Function

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:

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

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.

Constraints:

  • 1 <= url.length <= 10^4
  • url is guranteed to be a valid URL.
Assistant

Time: O(1)

Space: O(n)

import random

class Codec(object):
def init(self):
self.__random_length = 6
self.__tiny_url = "http://tinyurl.com/"
self.__alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
self.__lookup = {}

markdown
def encode(self, longUrl):
    """Encodes a URL to a shortened URL.

    :type longUrl: str
    :rtype: str
    """
    def getRand():
        rand = []
        for _ in range(self.__random_length):
            rand += self.__alphabet[random.randint(0, len(self.__alphabet)-1)]
        return "".join(rand)

    key = getRand()
    while key in self.__lookup:
        key = getRand()
    self.__lookup[key] = longUrl
    return self.__tiny_url + key

def decode(self, shortUrl):
    """Decodes a shortened URL to its original URL.

    :type shortUrl: str
    :rtype: str
    """
    return self.__lookup[shortUrl[len(self.__tiny_url):]]

from hashlib import sha256

class Codec2(object):

markdown
def __init__(self):
    self._cache = {}
    self.url = 'http://tinyurl.com/'

def encode(self, long_url):
    """Encodes a URL to a shortened URL.

    :type long_url: str
    :rtype: str
    """
    key = sha256(long_url.encode()).hexdigest()[:6]
    self._cache[key] = long_url
    return self.url + key

def decode(self, short_url):
    """Decodes a shortened URL to its original URL.

    :type short_url: str
    :rtype: str
    """
    key = short_url.replace(self.url, '')
    return self._cache[key]