@@ -0,0 +1,2102 @@
+package com.freetymekiyan.algorithms.level.medium;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Random;%0A%0A/**%0A * 535. Encode and Decode TinyURL%0A * %3Cp%3E%0A * Note: This is a companion problem to the System Design problem: Design TinyURL.%0A * TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it%0A * returns a short URL such as http://tinyurl.com/4e9iAk.%0A * %3Cp%3E%0A * Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode%0A * algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be%0A * decoded to the original URL.%0A * %3Cp%3E%0A * Related Topics: Hash Table, Math%0A * Similar Questions: (M) Design TinyURL%0A */%0Apublic class EncodeAndDecodeTinyURL %7B%0A%0A    public class Codec %7B%0A%0A        private static final String ALPHABET = %220123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%22;%0A        private Map%3CString, String%3E map = new HashMap%3C%3E();%0A%0A        // Encodes a URL to a shortened URL.%0A        public String encode(String longUrl) %7B%0A            String shortUrl;%0A            do %7B%0A                shortUrl = randomKey();%0A            %7D while (map.containsKey(shortUrl)); // Make sure the key doesn't duplicate with previous keys.%0A            map.put(shortUrl, longUrl);%0A            return shortUrl;%0A        %7D%0A%0A        // Decodes a shortened URL to its original URL.%0A        public String decode(String shortUrl) %7B%0A            return map.get(shortUrl);%0A        %7D%0A%0A        /**%0A         * Generate a random key/short url for an incoming long url.%0A         */%0A        private String randomKey() %7B%0A            StringBuilder sb = new StringBuilder();%0A            Random r = new Random(System.currentTimeMillis());%0A            for (int i = 0; i %3C 6; i++) %7B%0A                sb.append(ALPHABET.charAt(r.nextInt(ALPHABET.length())));%0A            %7D%0A            return sb.toString();%0A        %7D%0A    %7D%0A%0A// Your Codec object will be instantiated and called as such:%0A// Codec codec = new Codec();%0A// codec.decode(codec.encode(url));%0A%7D%0A

add 535. Encode and Decode TinyURL