Using Python hash function.

  • 0

    I am creating a hash of given url from the given function and then using it store the URL.

    class Codec:

    urlDict = {}
    def encode(self, longUrl):
        """Encodes a URL to a shortened URL.
        :type longUrl: str
        :rtype: str
        hsh = hash(longUrl)
        if hsh not in self.urlDict:
            self.urlDict[hsh] = longUrl
        return hsh
    def decode(self, shortUrl):
        """Decodes a shortened URL to its original URL.
        :type shortUrl: str
        :rtype: str
        return self.urlDict.get(shortUrl,"")

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.