2023-09-17 13:14:59 +0000 UTC

Encode and Decode TinyURL

Code

class Codec:

    def __init__(self):
        self.urls = {}
        self.id = 0

    def encode(self, longUrl: str) -> str:
        """Encodes a URL to a shortened URL.
        """
        id = self.id
        self.urls[id] = longUrl
        self.id += 1
        return id
        

    def decode(self, shortUrl: str) -> str:
        """Decodes a shortened URL to its original URL.
        """
        return self.urls[int(shortUrl)]
        

# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))