forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kevin Naughton Jr
authored and
Kevin Naughton Jr
committed
May 29, 2018
1 parent
76c33ea
commit 547272f
Showing
6 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |