public class Codec {
  private Map<String, String> urlMapper = new HashMap<>();
  private Random rand = new Random();
  private byte[] stringArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".getBytes();

  // Encodes a URL to a shortened URL.
  public String encode(String longUrl) {
    String shortened = getShortUrl(longUrl);
    urlMapper.put(shortened, longUrl);
    return shortened;
  }

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


@@ -14,16 +14,18 @@
 Codec %7B%0A
+  
   privat
@@ -71,24 +71,26 @@
 ashMap%3C%3E();%0A
+  
   private Ra
@@ -115,16 +115,18 @@
 ndom();%0A
+  
   privat
@@ -226,16 +226,251 @@
 tes();%0A%0A
+    private String getShortUrl() %7B%0A      byte%5B%5D s = new byte%5B6%5D;%0A      for (int i = 0; i %3C 6; i++) %7B%0A        int n = rand.nextInt(62);%0A        s%5Bi%5D = stringArray%5Bn%5D;%0A      %7D%0A      return %22http://tinyurl.com/%22 + new String(s);%0A    %7D%0A%0A  
   // Enc
@@ -496,24 +496,26 @@
 rtened URL.%0A
+  
   public Str
@@ -539,24 +539,28 @@
  longUrl) %7B%0A
+    
     String s
@@ -574,69 +574,280 @@
 d = 
-getShortUrl(longUrl);%0A    urlMapper.put(shortened, longUrl);%0A
+null;%0A        int count = 0;%0A        while (count %3C 10) %7B%0A            shortened = getShortUrl();%0A            if (urlMapper.get(shortened) == null) %7B%0A                urlMapper.put(shortened, longUrl);%0A                break;%0A            %7D%0A            count++;%0A        %7D%0A    
     
@@ -864,21 +864,25 @@
 rtened;%0A
+  
   %7D%0A%0A
+  
   // Dec
@@ -925,16 +925,18 @@
  URL.%0A  
+ 
+ 
 public S
@@ -963,24 +963,28 @@
 shortUrl) %7B%0A
+    
     return u
@@ -1007,15 +1007,17 @@
 rtUrl);%0A
+  
   %7D%0A%7D%0A%0A

Update java solution for 'Encode and Decode TinyURL'