message: Add alternative solution for 'Encode and decode tiny url' problem
diff: # You just need to ensure that a URL can be encoded to a tiny URL
  # and the tiny URL can be decoded to the original URL.
  +
  class Codec:
  def __init__(self):
  self.__random_length = 6
  @@ -17,7 +18,6 @@ class Codec:
  self.__alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  self.__lookup = {}
  -
  def encode(self, longUrl):
  """Encodes a URL to a shortened URL.
  @@ -36,7 +36,6 @@ class Codec:
  self.__lookup[key] = longUrl
  return self.__tiny_url + key
  -
  def decode(self, shortUrl):
  """Decodes a shortened URL to its original URL.
  @@ -46,6 +45,34 @@ class Codec:
  return self.__lookup[shortUrl[len(self.__tiny_url):]]
  +from hashlib import sha256
  +
  +
  +class Codec2:
  +
  + def __init__(self):
  + self._cache = {}
  + self.url = 'http://tinyurl.com/'
  +
  + def encode(self, long_url):
  + """Encodes a URL to a shortened URL.
  +
  + :type long_url: str
  + :rtype: str
  + """
  + key = sha256(long_url.encode()).hexdigest()[:6]
  + self._cache[key] = long_url
  + return self.url + key
  +
  + def decode(self, short_url):
  + """Decodes a shortened URL to its original URL.
  +
  + :type short_url: str
  + :rtype: str
  + """
  + key = short_url.replace(self.url, '')
  + return self._cache[key]
  +
  # Your Codec object will be instantiated and called as such:
  # codec = Codec()
  # codec.decode(codec.encode(url))
  