package leetcode;

/**
 * https://leetcode.com/problems/encode-and-decode-tinyurl/
 */
public class Problem535 {
    public static class Codec {
        // Encodes a URL to a shortened URL.
        public String encode(String longUrl) {
            // TODO
            return "";
        }

        // Decodes a shortened URL to its original URL.
        public String decode(String shortUrl) {
            // TODO
            return "";
        }
    }

    public static void main(String[] args) {
        Codec codec = new Codec();
        codec.decode(codec.encode("https://leetcode.com/problems/design-tinyurl"));
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -199,133 +199,877 @@
     
-// Encodes a URL to a shortened URL.%0A        public String encode(String longUrl) %7B%0A            // TODO%0A            return %22%22
+private static final String PREFIX_URL = %22http://tinyurl.com/%22;%0A        private static final String ALPHANUMERIC = %22abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789%22;%0A        private Map%3CInteger, String%3E map = new HashMap%3C%3E();%0A        private int id;%0A%0A        // Encodes a URL to a shortened URL.%0A        public String encode(String longUrl) %7B%0A            StringBuilder shortUrl = new StringBuilder();%0A            int key = id;%0A            if (key == 0) %7B%0A                shortUrl.append(ALPHANUMERIC.charAt(key %25 ALPHANUMERIC.length()));%0A            %7D else %7B%0A                while (key %3E 0) %7B%0A                    shortUrl.append(ALPHANUMERIC.charAt(key %25 ALPHANUMERIC.length()));%0A                    key = key / ALPHANUMERIC.length();%0A                %7D%0A            %7D%0A            map.put(id++, longUrl);%0A            return PREFIX_URL + shortUrl.toString()
 ;%0A  
@@ -1197,15 +1197,61 @@
     
-// TODO
+String url = shortUrl.substring(PREFIX_URL.length());
 %0A   
@@ -1263,17 +1263,19 @@
     
-return %22%22
+int key = 0
 ;%0A  
@@ -1284,180 +1284,259 @@
     
-%7D%0A
     
-%7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Codec codec = new Codec();%0A        codec.decode(codec.encode(%22https://leetcode.com/problems/design-tinyurl%22));
+System.out.println(shortUrl);%0A            for (int i = url.length() - 1; i %3E= 0; i--) %7B%0A                key += ALPHANUMERIC.indexOf(url.charAt(i)) * (int) Math.pow(ALPHANUMERIC.length(), i);%0A            %7D%0A            return map.get(key);%0A        %7D
 %0A   

Solve problem 535