Encode and Decode TinyURL


  • 0

    Click here to see the full article post


  • 0

    Using a combination of System.nanoTime() and hashCode().
    It produces urls in this format https://tinyurl.com/596293762700519

    // Encodes a URL to a shortened URL.
    HashMap<String,String> tinyUrlMap = new HashMap<>();
    HashMap<String,String> longUrlMap = new HashMap<>();
    String tinyPrefix = "https://tinyurl.com/";

    public String encode(String longUrl) {
    if(longUrlMap.containsKey(longUrl)) return longUrlMap.get(longUrl);
    String tinyUrl = tinyPrefix+(longUrl.hashCode()+System.nanoTime());
    tinyUrlMap.put(tinyUrl,longUrl);
    longUrlMap.put(longUrl,tinyUrl);
    return tinyUrl;
    }

    public String decode(String shortUrl) {
    return tinyUrlMap.get(shortUrl);
    }


  • 0
    F
    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        return longUrl;
    }
    
    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        return shortUrl;
    }
    

    is Accepted ;)


  • 0
    R

    Do we need to de-dupe the long url? Can one same long url have multiple tinyUrls?


  • 0
    G

    Great Step by Step Solution


  • 0
    W

    The number of URLs that can be encoded is quite large in this case, nearly of the order (10 + 26 *2)^6. Why this number?


  • 0
    M

    @workSmart 10 digits, 26 upper and 26 lower letters, length is 6.


  • 1
    class Codec:
    
        def __init__(self, prefix = 'http://tinyurl.com/', \
        	         chars = 6, encodingChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'):
          self.__shortToLong = {}
          self.__longToshort = {}
          self.__urlChars = chars
          self.__prefix = prefix
          self.__encodingChars = encodingChars
    
        def __shortUrlGeneratorV1(self, longUrl):
          return str(len(self.__shortToLong))
    
        def __shortUrlGeneratorV2(self, longUrl):
          urlInt = len(self.__shortToLong) + int(10 ** self.__urlChars * random.random())
          shortUrl = str(self.__convertToEncodingChars(urlInt))
          return shortUrl
    
        def __convertToEncodingChars(self, intUrl):
          size = len(self.__encodingChars)
          encodeUrl = ''
    
          while intUrl > 0:
            encodeUrl += self.__encodingChars[intUrl % size]
            intUrl /= size
    
          return encodeUrl
    
        def encode(self, longUrl):
            """Encodes a URL to a shortened URL.
            
            :type longUrl: str
            :rtype: str
            """
            if longUrl in self.__longToshort:
              return self.__prefix + self.__longToshort[longUrl]
    
            shortUrl = self.__shortUrlGeneratorV2(longUrl)
            
            while shortUrl in self.__shortToLong:
              shortUrl = self.__shortUrlGeneratorV2(longUrl)
    
            self.__shortToLong[shortUrl] = longUrl
            self.__longToshort[longUrl]  = shortUrl
            # print self.__shortToLong
            return self.__prefix + shortUrl
    
        def decode(self, shortUrl):
            """Decodes a shortened URL to its original URL.
            
            :type shortUrl: str
            :rtype: str
            """
            longUrl = self.__shortToLong[shortUrl[len(self.__prefix):]]
            return longUrl
    

Log in to reply
 

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