import random
import string
class Codec:
def __init__(self):
self.url2code = {}
self.code2url = {}
self.chars = string.ascii_letters + string.digits
self.prefix = "http://tinyurl.com/"
def encode(self, longUrl: str) -> str:
if longUrl in self.url2code:
return self.prefix + self.url2code[longUrl]
while True:
code = ''.join(random.choices(self.chars, k=6))
if code not in self.code2url:
break
self.code2url[code] = longUrl
self.url2code[longUrl] = code
return self.prefix + code
def decode(self, shortUrl: str) -> str:
code = shortUrl.replace(self.prefix, '')
return self.code2url.get(code, "")
class Solution {
public:
unordered_map<string, string> code2url, url2code;
string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string prefix = "http://tinyurl.com/";
string getCode() {
string code = "";
for (int i = 0; i < 6; ++i) {
code += chars[rand() % chars.size()];
}
return code;
}
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
if (url2code.count(longUrl)) {
return prefix + url2code[longUrl];
}
string code = getCode();
while (code2url.count(code)) {
code = getCode();
}
code2url[code] = longUrl;
url2code[longUrl] = code;
return prefix + code;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
string code = shortUrl.substr(prefix.size());
if (code2url.count(code)) return code2url[code];
return "";
}
};
public class Codec {
Map<String, String> code2url = new HashMap<>();
Map<String, String> url2code = new HashMap<>();
String chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String prefix = "http://tinyurl.com/";
Random rand = new Random();
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
if (url2code.containsKey(longUrl)) {
return prefix + url2code.get(longUrl);
}
String code;
do {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; ++i) {
sb.append(chars.charAt(rand.nextInt(chars.length())));
}
code = sb.toString();
} while (code2url.containsKey(code));
code2url.put(code, longUrl);
url2code.put(longUrl, code);
return prefix + code;
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
String code = shortUrl.replace(prefix, "");
return code2url.getOrDefault(code, "");
}
}
var Codec = function() {
this.code2url = {};
this.url2code = {};
this.chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
this.prefix = 'http://tinyurl.com/';
};
Codec.prototype.encode = function(longUrl) {
if (this.url2code[longUrl]) {
return this.prefix + this.url2code[longUrl];
}
let code = '';
do {
code = '';
for (let i = 0; i < 6; ++i) {
code += this.chars[Math.floor(Math.random() * this.chars.length)];
}
} while (this.code2url[code]);
this.code2url[code] = longUrl;
this.url2code[longUrl] = code;
return this.prefix + code;
};
Codec.prototype.decode = function(shortUrl) {
let code = shortUrl.replace(this.prefix, '');
return this.code2url[code] || '';
};
encode(longUrl)
: Converts a long URL to a shortened URL.decode(shortUrl)
: Converts the shortened URL back to the original long URL.code2url
.encode("https://leetcode.com/problems/design-tinyurl")
abc123
.url2code["https://leetcode.com/problems/design-tinyurl"] = "abc123"
code2url["abc123"] = "https://leetcode.com/problems/design-tinyurl"
"http://tinyurl.com/abc123"
.decode("http://tinyurl.com/abc123")
"abc123"
and look it up in code2url
to get the original URL.url2code
mapping.