Easy Encode Python Solution(52ms)


  • 0
    N
    import random
    class Codec:
        randomKey = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        urlDict = {}
    
        def encode(self, longUrl):
            shortUrl = self.tinyUrl()
            self.urlDict[shortUrl] = longUrl
            return shortUrl
    
        def tinyUrl(self):
            tinyUrl = "http://tinyurl.com/"
            for i in range(6):
                tinyUrl += self.randomKey[random.randint(0,61)]
    
            if tinyUrl in self.urlDict:
                self.tinyUrl()
            else:
                return tinyUrl
    
        def decode(self, shortUrl):
            return self.urlDict[shortUrl]

Log in to reply
 

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