Tiny URL II

As a follow up for Tiny URL, we are going to support custom tiny url, so that user can create their own tiny url. That is to say, you need to implement one more createCustom than 232. Tiny Url.

You should implement three methods:

  • longToShort(url) Convert a long url to a short url which starts with http://tiny.url/.
  • shortToLong(url) Convert a short url to a long url.
  • createCustom(url, key) Set the short url of a long url to http://tiny.url/ + key

You can design any shorten algorithm, the judge only cares about:

  1. The length of short key' generated by longToShort should equal to 6 (without domain and slash). And the acceptable characters are [a-zA-Z0-9]. For example: abcD9E
  2. No two long urls mapping to the same short url and no two short urls mapping to the same long url.
  3. If createCustom can not meet users' expectment, return "error"; otherwise return the short url.

Example

Example 1:

Input:
  createCustom("http://www.lintcode.com/", "lccode")
  shortToLong("http://tiny.url/lccode")
  createCustom("http://www.lintcode.com/", "ltcode")
Output:
  "http://tiny.url/lccode"
  "http://www.lintcode.com/"
  "error"

Example 2:

Input:
  longToShort("http://www.lintcode.com/")
  createCustom("http://www.lintcode.com/", "ltcode")
Output:
  "http://tiny.url/abcdef"    => This answer is not unique.
  "error"
Explanation:
  Although it is almost impossible:
  if your longToShort() just returns "http://tiny.url/ltcode",  
  your createCustom() should return "http://tiny.url/ltcode".

思路:需要额外处理的便是用户设定的网址与已有的冲突时, 需要返回 "error".

但是注意这个细节: 如果用户设定的和已有的恰好相同, 那么同样应该返回短网址.

public class TinyUrl2 {
    /*
     * @param long_url: a long url
     * @param key: a short key
     * @return: a short url starts with http://tiny.url/
     */
    private String prefix = "http://tiny.url/";
    private HashMap<String, String> shortToLongMap = new HashMap<String, String>();
    private HashMap<String, String> longtoShortMap = new HashMap<String, String>();
    private String SPACE = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    public String createCustom(String longURL, String key) {
        String shortURL = prefix + key;
        if(longtoShortMap.containsKey(longURL)){
            if(longtoShortMap.get(longURL).equals(shortURL)){
                return shortURL;
            } else {
                return "error";
            }
        }
        if(shortToLongMap.containsKey(shortURL)) {
            return "error";
        }
        longtoShortMap.put(longURL, shortURL);
        shortToLongMap.put(shortURL, longURL);
        return shortURL;
    }

    /*
     * @param long_url: a long url
     * @return: a short url starts with http://tiny.url/
     */
    public String longToShort(String url) {
       if(longtoShortMap.containsKey(url)){
            return longtoShortMap.get(url);
        }
        String shortURL = generateRandomShortURL();
        longtoShortMap.put(url, shortURL);
        shortToLongMap.put(shortURL, url);
        return shortURL;
    }
    
    private String generateRandomShortURL() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        while(true) {
            int count = 6;
            while(count > 0) {
                sb.append(SPACE.charAt(random.nextInt(62)));
                count--;
            }
            String shortURL = prefix + sb.toString();
            if(shortToLongMap.containsKey(shortURL)) {
                sb = new StringBuilder();
            } else {
                return shortURL;
            }
        }
    }

    /*
     * @param short_url: a short url starts with http://tiny.url/
     * @return: a long url
     */
    public String shortToLong(String url) {
         if(shortToLongMap.containsKey(url)){
            return shortToLongMap.get(url);
        }
        return "error";
    }
}

 

Tiny URL IITiny URL II flyatcmu 发布了569 篇原创文章 · 获赞 13 · 访问量 17万+ 他的留言板 关注
上一篇:Tiny Web服务器代码分析


下一篇:youtube-dl 进程间通信实战