Design Tiny URL
Given a long url, make it shorter. To make it simpler, let's ignore the domain name.
You should implement two methods:
longToShort(url). Convert a long url to a short url.shortToLong(url). Convert a short url to a long url starts withhttp://tiny.url/.
You can design any shorten algorithm, the judge only cares about two things:
The
short key's length shouldequal to 6(without domain and slash). And the acceptable characters are[a-zA-Z0-9]. For example:abcD9ENo two long urls mapping to the same short url and no two short urls mapping to the same long url.
Example
Given url =http://www.lintcode.com/faq/?id=10, run the following code (or something similar):
short_url = longToShort(url) // may return http://tiny.url/abcD9E
long_url = shortToLong(short_url) // return http://www.lintcode.com/faq/?id=10The short_url you return should be unique short url and start withhttp://tiny.url/and6acceptable characters. For example "http://tiny.url/abcD9E" or something else.
The long_url should behttp://www.lintcode.com/faq/?id=10in this case.
Note
两种方法,随机或者base62
都要建立long和short的映射关系
Code
Last updated