[Leetcode] Encode and Decode TinyURL encode and decode small URL addresses

Source: Internet
Author: User

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 as and https://leetcode.com/problems/design-tinyurl it returns a short URL such as http://tinyurl.com/4e9iAk .

Design the and methods for the encode decode tinyurl service. There is no restriction on what your encode/decode algorithm should work. You just need to ensure, that a URL can is encoded to a tiny URL and the tiny URL can is decoded to the original URL.

This problem lets us encode and decode the thin URL address, which is actually very useful, because some of the link address is particularly long, very annoying, if it can be reduced to a fixed length, it is very refreshing. The simplest kind of encoding is to use a counter, currently the number of the URL is encoded into a few, and then decoding the time can also be based on the number to find the original URL, see the code is as follows:

Solution One:

classSolution { Public:    //encodes a URL to a shortened URL.    stringEncodestringLongurl)        {Url.push_back (Longurl); return "http://tinyurl.com/"+ to_string (url.size ()-1); }    //decodes a shortened URL to its original URL.    stringDecodestringShorturl) {Auto pos= Shorturl.find_last_of ("/"); returnUrl[stoi (SHORTURL.SUBSTR (pos +1))]; }    Private: Vector<string>URL;};

Although the above method is simple, but the disadvantage is many, first, if you accept the same URL address multiple times, will still be the wrong URL to handle. Of course, this disadvantage can be changed by the vector to a hash table, each time first find whether the URL already exists. Although this drawback can be overcome, but because of the counter code, then the current server storage of how many URLs exposed, perhaps there will be security risks. And the other disadvantage of the counter code is that the number will continue to increase, then the length of the encoding is not determined. and the topic explicitly recommended the use of six-bit random character encoding, then we just have to randomly generate 6 characters in all uppercase and lowercase letters and numbers, we use a hash table to establish a mapping between 6-bit characters and URLs, if the randomly generated characters have existed before, we continue to randomly generate new strings, Until you have generated a string that you didn't have before. The following code uses two hash tables in order to establish a mapping between the six-bit random string and the URL, so that when a large number of identical URLs are entered, no new random strings are generated. Of course, do not add this function can also pass the OJ, the problem of the OJ is basically the shape of a dummy, two functions are directly returned to the parameter string can also pass OJ, embarrassed ~

Solution Two:

classSolution { Public: Solution () {Dict="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";        Short2long.clear ();        Long2short.clear ();    Srand (Time (NULL)); }    //encodes a URL to a shortened URL.    stringEncodestringLongurl) {        if(Long2short.count (Longurl)) {return "http://tinyurl.com/"+Long2short[longurl]; }        intIDX =0, val =0; stringRandstr;  for(inti =0; I <6; ++i) Randstr.push_back (Dict[rand ()% +]);  while(Short2long.count (randstr)) {Randstr[idx]= Dict[rand ()% +]; IDX= (idx +1) %5; } Short2long[randstr]=Longurl; Long2short[longurl]=Randstr; return "http://tinyurl.com/"+Randstr; }    //decodes a shortened URL to its original URL.    stringDecodestringShorturl) {        stringRandstr = Shorturl.substr (shorturl.find_last_of ("/") +1); returnShort2long.count (RANDSTR)?Short2long[randstr]: shorturl; }    Private: Unordered_map<string,string>Short2long, Long2short; stringdict;};

Resources:

Https://discuss.leetcode.com/topic/81637/two-solutions-and-thoughts/2

Https://discuss.leetcode.com/topic/81736/c-solution-using-random-just-for-fun

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Encode and Decode tinyurl encode and decode small URL addresses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.