Skip to content

Commit

Permalink
add EncodeAndDecodeTinyURL.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Naughton Jr authored and Kevin Naughton Jr committed May 29, 2018
1 parent 76c33ea commit 547272f
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 0 deletions.
41 changes: 41 additions & 0 deletions company/amazon/EncodeAndDecodeTinyURL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//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.

public class EncodeAndDecodeTinyURL {
HashMap<String, String> map = new HashMap<String, String>();
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count = 1;

public String getKey() {
String key = "";
while(count > 0) {
count--;
key += characters.charAt(count);
count /= characters.length();
}

return key;
}

// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = getKey();
map.put(key, longUrl);
count++;

return "http://tinyurl.com/" + key;
}

// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
41 changes: 41 additions & 0 deletions company/facebook/EncodeAndDecodeTinyURL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//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.

public class EncodeAndDecodeTinyURL {
HashMap<String, String> map = new HashMap<String, String>();
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count = 1;

public String getKey() {
String key = "";
while(count > 0) {
count--;
key += characters.charAt(count);
count /= characters.length();
}

return key;
}

// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = getKey();
map.put(key, longUrl);
count++;

return "http://tinyurl.com/" + key;
}

// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
41 changes: 41 additions & 0 deletions company/google/EncodeAndDecodeTinyURL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//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.

public class EncodeAndDecodeTinyURL {
HashMap<String, String> map = new HashMap<String, String>();
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count = 1;

public String getKey() {
String key = "";
while(count > 0) {
count--;
key += characters.charAt(count);
count /= characters.length();
}

return key;
}

// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = getKey();
map.put(key, longUrl);
count++;

return "http://tinyurl.com/" + key;
}

// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
41 changes: 41 additions & 0 deletions company/uber/EncodeAndDecodeTinyURL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//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.

public class EncodeAndDecodeTinyURL {
HashMap<String, String> map = new HashMap<String, String>();
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count = 1;

public String getKey() {
String key = "";
while(count > 0) {
count--;
key += characters.charAt(count);
count /= characters.length();
}

return key;
}

// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = getKey();
map.put(key, longUrl);
count++;

return "http://tinyurl.com/" + key;
}

// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
41 changes: 41 additions & 0 deletions leetcode/hash-table/EncodeAndDecodeTinyURL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//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.

public class EncodeAndDecodeTinyURL {
HashMap<String, String> map = new HashMap<String, String>();
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count = 1;

public String getKey() {
String key = "";
while(count > 0) {
count--;
key += characters.charAt(count);
count /= characters.length();
}

return key;
}

// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = getKey();
map.put(key, longUrl);
count++;

return "http://tinyurl.com/" + key;
}

// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
41 changes: 41 additions & 0 deletions leetcode/math/EncodeAndDecodeTinyURL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//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.

public class EncodeAndDecodeTinyURL {
HashMap<String, String> map = new HashMap<String, String>();
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count = 1;

public String getKey() {
String key = "";
while(count > 0) {
count--;
key += characters.charAt(count);
count /= characters.length();
}

return key;
}

// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = getKey();
map.put(key, longUrl);
count++;

return "http://tinyurl.com/" + key;
}

// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(shortUrl.replace("http://tinyurl.com/", ""));
}
}

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

0 comments on commit 547272f

Please sign in to comment.