Python Solution

  • 1
    class Codec:
        def __init__(self):
            self.d = {}
            self.r = {}
        def encode(self, longUrl):
            self.d[longUrl] = longUrl.__hash__()
            self.r[longUrl.__hash__()] = longUrl
            return longUrl.__hash__()
        def decode(self, shortUrl):
            return self.r[shortUrl]

  • 0

    I think this is incorrect because two strings can generate same hash. But we want short url to be unique.

Log in to reply

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