-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-535-Encode-and-Decode-TinyURL.java
More file actions
37 lines (32 loc) · 1.24 KB
/
LeetCode-535-Encode-and-Decode-TinyURL.java
File metadata and controls
37 lines (32 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 1. Using Base64 encoding algorithm to encode the longUrl to tinyUrl.
/*
https://leetcode.com/problems/encode-and-decode-tinyurl/discuss/100268/Two-solutions-and-thoughts
*/
public class Codec {
HashMap<String, String> map = new HashMap<>(); // store <shortUrl, longUrl> pair
String BASE_HOST = "http://tinyurl.com/";
String charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random rand = new Random();
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = "";
do {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
int r = rand.nextInt(charSet.length());
sb.append(charSet.charAt(r));
}
key = sb.toString();
} while (map.containsKey(key));
map.put(BASE_HOST + key, longUrl);
return BASE_HOST + key;
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
if (!map.containsKey(shortUrl)) return "";
return map.get(shortUrl);
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));