• [x] ### Random
  • [x] ### HashMap
  • [x] ### Interger.valueOf, String.valueOf

535.Encode and Decode TinyURL

Note: This is a companion problem to the
System Design
problem:
Design TinyURL

TinyURL is a URL shortening service where you enter a URL such ashttps://leetcode.com/problems/design-tinyurland it returns a short URL such ashttp://tinyurl.com/4e9iAk.

Design theencodeanddecodemethods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. 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.

  • [x] use two hashmap to store the tiny url and origin url
  • use random class in java to generate the length six random tiny url
public class Codec {
    HashMap<String, String> longToShort = new HashMap<>();
    HashMap<String, String> shortToLong = new HashMap<>();
    String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz1234567890";
    Random random = new Random();
    String basecase = "http://domain.com/";
    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        if (longToShort.containsKey(longUrl)) {
            return longToShort.get(longUrl);
        }
        StringBuilder sb = new StringBuilder();
        do {
           for (int i = 0; i < 6; i++) {
               int num = random.nextInt(characters.length());
               sb.append(characters.charAt(num));
           }
        }while (longToShort.containsKey(sb.toString()));
        longToShort.put(longUrl, sb.toString());
        shortToLong.put(sb.toString(), longUrl);
        return basecase + sb.toString();
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        return shortToLong.get(shortUrl.substring(basecase.length()));
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url))
public class Codec {
    List<String> urls = new ArrayList<String>();
    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        urls.add(longUrl);
        return String.valueOf(urls.size()-1);
    }

d    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        int index = Integer.valueOf(shortUrl);
        return (index<urls.size())?urls.get(index):"";
    }
}

below is the tiny url solution in java, also this is the similar method in industry. In industry, most of shorten url service is by database, one auto increasing long number as primary key. whenever a long url need to be shorten, append to the database, and return the primary key number. (the database is very easy to distribute to multiple machine like HBase, or even you can use the raw file system to store data and improve performance by shard and replica).

Note, it’s meaningless to promise the same long url to be shorten as the same short url. if you do the promise and use something like hash to check existing, the benefit is must less than the cost.

Note: if you want the shorted url contains ‘0-9a-zA-Z’ instead of ‘0-9’, then you need to use 62 number system, not 10 number system(decimal) to convert the primary key number. like 123->‘123’ in decimal,

123->‘1Z’ in 62 number system (or ‘0001Z’ for align).

public class Codec {
    List<String> urls= new ArrayList<String>();
    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        urls.add(longUrl);
        System.out.println(String.valueOf(urls.size() - 1)); //store the string version of index in array
        return String.valueOf(urls.size() - 1);
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        int index = Integer.valueOf(shortUrl);//retrieve the int version of index 
        System.out.println(index);
        return (index<urls.size())?urls.get(index):"";
    }
}

results matching ""

    No results matching ""