535. Encode and Decode TinyURL

Description

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

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

Solution

Count

public class Codec {
    private Map idToLong = new HashMap<>();
    private int id = 0;

    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        idToLong.put(id, longUrl);
        return "http://tinyurl.com/" + id++;
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        int id = Integer.parseInt(shortUrl.replace("http://tinyurl.com/", ""));
        return idToLong.get(id);
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));

Random

Using increasing numbers as codes like that is simple but has some disadvantages, which the below solution fixes:

  • If I’m asked to encode the same long URL several times, it will get several entries. That wastes codes and memory.
  • People can find out how many URLs have already been encoded. Not sure I want them to know.
  • People might try to get special numbers by spamming me with repeated requests shortly before their desired number comes up.
  • Only using digits means the codes can grow unnecessarily large. Only offers a million codes with length 6 (or smaller). Using six digits or lower or upper case letters would offer (10+26*2)6 = 56,800,235,584 codes with length 6.

The following solution doesn’t have these problems. It produces short URLs like http://tinyurl.com/KtLa2U, using a random code of six digits or letters. If a long URL is already known, the existing short URL is used and no new entry is generated.

It’s possible that a randomly generated code has already been generated before. In that case, another random code is generated instead. Repeat until we have a code that’s not already in use. How long can this take? Well, even if we get up to using half of the code space, which is a whopping 626/2 = 28,400,117,792 entries, then each code has a 50% chance of not having appeared yet. So the expected/average number of attempts is 2, and for example only one in a billion URLs takes more than 30 attempts. And if we ever get to an even larger number of entries and this does become a problem, then we can just use length 7. We’d need to anyway, as we’d be running out of available codes.

public class Codec {
    private static char[] arr = ("abcdefghijklmnopqrstuvwxyz"
        + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789").toCharArray();
    private static final int SHORT_URL_LEN = 6;
    private static final String BASE_URL = "http://tinyurl.com/";
    private Map id2LongUrl;
    private Map longUrl2Id;
    private Random random;
    
    public Codec() {
        id2LongUrl = new HashMap<>();
        longUrl2Id = new HashMap<>();
        random = new Random();
    }
    
    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        if (!longUrl2Id.containsKey(longUrl)) {     // longUrl already stored
            int id = -1;
            
            while (id < 0 || id2LongUrl.containsKey(id)) {  // avoid conflict
                id = random.nextInt((int) Math.pow(arr.length, SHORT_URL_LEN) - 1);
            }
            
            longUrl2Id.put(longUrl, id);
            id2LongUrl.put(id, longUrl);
        }

        int id = longUrl2Id.get(longUrl);
        StringBuilder sb = new StringBuilder();
        
        for (int i = 0; i < SHORT_URL_LEN; ++i) {
            sb.insert(0, arr[id % arr.length]);
            id /= arr.length;
        }
        
        return BASE_URL + sb.toString();
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        char[] shortUrlArr = shortUrl.replace(BASE_URL, "").toCharArray();
        int id = 0;
        
        for (int i = 0; i < SHORT_URL_LEN; ++i)
        {
            if ('a' <= shortUrlArr[i] && shortUrlArr[i] <= 'z')
              id = id * 62 + shortUrlArr[i] - 'a';
            if ('A' <= shortUrlArr[i] && shortUrlArr[i] <= 'Z')
              id = id * 62 + shortUrlArr[i] - 'A' + 26;
            if ('0' <= shortUrlArr[i] && shortUrlArr[i] <= '9')
              id = id * 62 + shortUrlArr[i] - '0' + 52;
        }
        
        return id2LongUrl.get(id);
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));

你可能感兴趣的:(535. Encode and Decode TinyURL)