File size: 3,115 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "cp-10041", "name": "LeetCode 0535: Encode And Decode Tinyurl"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Encode and Decode TinyURL (Difficulty: Medium)\nTags: Hash Table, String, Design, Hash Function\n\nTinyURL 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.\nThere 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.\nImplement the Solution class:\n\nExample 1:\nInput: url = \"https://leetcode.com/problems/design-tinyurl\"\nOutput: \"https://leetcode.com/problems/design-tinyurl\"\n\nExplanation:\nSolution obj = new Solution();\nstring tiny = obj.encode(url); // returns the encoded tiny url.\nstring ans = obj.decode(tiny); // returns the original url after decoding it.\n\nConstraints:\n- 1 <= url.length <= 10^4\n- url is guranteed to be a valid URL."}}
{"type": "message", "message": {"role": "assistant", "content": "# Time:  O(1)\n# Space: O(n)\n\nimport random\n\n\nclass Codec(object):\n    def __init__(self):\n        self.__random_length = 6\n        self.__tiny_url = \"http://tinyurl.com/\"\n        self.__alphabet = \"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n        self.__lookup = {}\n\n    def encode(self, longUrl):\n        \"\"\"Encodes a URL to a shortened URL.\n\n        :type longUrl: str\n        :rtype: str\n        \"\"\"\n        def getRand():\n            rand = []\n            for _ in range(self.__random_length):\n                rand += self.__alphabet[random.randint(0, len(self.__alphabet)-1)]\n            return \"\".join(rand)\n\n        key = getRand()\n        while key in self.__lookup:\n            key = getRand()\n        self.__lookup[key] = longUrl\n        return self.__tiny_url + key\n\n    def decode(self, shortUrl):\n        \"\"\"Decodes a shortened URL to its original URL.\n\n        :type shortUrl: str\n        :rtype: str\n        \"\"\"\n        return self.__lookup[shortUrl[len(self.__tiny_url):]]\n\n\nfrom hashlib import sha256\n\n\nclass Codec2(object):\n\n    def __init__(self):\n        self._cache = {}\n        self.url = 'http://tinyurl.com/'\n\n    def encode(self, long_url):\n        \"\"\"Encodes a URL to a shortened URL.\n\n        :type long_url: str\n        :rtype: str\n        \"\"\"\n        key = sha256(long_url.encode()).hexdigest()[:6]\n        self._cache[key] = long_url\n        return self.url + key\n\n    def decode(self, short_url):\n        \"\"\"Decodes a shortened URL to its original URL.\n\n        :type short_url: str\n        :rtype: str\n        \"\"\"\n        key = short_url.replace(self.url, '')\n        return self._cache[key]\n\n\n"}}