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.
1 Public classCodec {2 Private Const stringPref ="http://tinyurl.com/";3 4 //In thoery should use a hash founction5 Private intCounter =0;6 Privatedictionary<int,string> Hashtourl =Newdictionary<int,string>();7 Privatedictionary<string,int> Urltohash =Newdictionary<string,int>();8 9 //encodes a URL to a shortened URLTen Public stringEncodestringLongurl) { One if(!Urltohash.containskey (longurl)) A { -Urltohash[longurl] =counter; -Hashtourl[counter] =Longurl; thecounter++; - } - - returnPref +Urltohash[longurl]; + } - + //decodes a shortened URL to its original URL. A Public stringDecodestringShorturl) { at inthash = Int32.Parse (shorturl.substring ( +, Shorturl.length- +)); - - returnHashtourl[hash]; - } - } - in //Your Codec object would be instantiated and called as such: - //Codec Codec = new Codec (); to //codec.decode (Codec.encode (URL));
Leetcode 535:encode and Decode tinyurl