A unique auto-increment for longUrl


  • 0
    P
    class Codec:
        def __init__(self):
            
            self.dict1 = {}
            self.i=0
            
        def encode(self, longUrl):
            """Encodes a URL to a shortened URL.
            
            :type longUrl: str
            :rtype: str
            """
            if longUrl not in self.dict1:
                self.i+=1
                self.dict1[longUrl] = str(self.i)
            return self.dict1[longUrl]
            
    
        def decode(self, shortUrl):
            """Decodes a shortened URL to its original URL.
            
            :type shortUrl: str
            :rtype: str
            """
            for lu, su in self.dict1.items():
                if su == shortUrl:
                    return lu
            return null
    

Log in to reply
 

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