Last active
August 29, 2015 14:05
-
-
Save KodeSeeker/dd3783687788db5f24a9 to your computer and use it in GitHub Desktop.
Design a TinyURL service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Assume that the URL's shortened by the service is limited by INT_MAX. | |
| * */ | |
| public class TinyURL | |
| { | |
| public static int urlID=0; // max number of URL's shortened by this service. is limited by INT_MAX | |
| public static const int URL_LENGTH=36; | |
| //Maps to simulate a database. | |
| public Map<String,String> map= new HashMap<String,String>(); // maps base36 to URL. | |
| public Map< String,Integer> countURL= new HashMap<String,Integer>(); | |
| //encode the URL | |
| public static String encode(String URL) | |
| { | |
| //perform URL sanitization. | |
| int id; | |
| if (countURL.contains(URL)) | |
| { | |
| // we already have a number for this URL. | |
| id= countURL.get(URL); | |
| } | |
| else | |
| { | |
| id=++urlID; | |
| } | |
| map.put(Integer(i,URL_LENGTH),URL);// this way to help with decode | |
| return Integer(id,URL_LENGTH).toString();// base 36 version of the ID. | |
| } | |
| public String decode (String compress) | |
| { | |
| if (compress==null) | |
| return null; | |
| if(map.contains(compress))// compress is the base36 version | |
| { | |
| return map.get(compress); //should return the URL. | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment