forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 15
/
encode-and-decode-tinyurl.cpp
42 lines (36 loc) · 1.13 KB
/
encode-and-decode-tinyurl.cpp
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
38
39
40
41
42
// Time: O(1)
// Space: O(n)
class Solution {
public:
Solution() : gen_((random_device())()) {
}
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
string key = getRand();
while (lookup_.count(key)) {
key = getRand();
}
lookup_[key] = longUrl;
return "http://tinyurl.com/" + key;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return lookup_[shortUrl.substr(tiny_url.length())];
}
private:
string getRand() {
string random;
for (int i = 0; i < random_length; ++i) {
random += alphabet_[uniform_int_distribution<int>{0, alphabet_.length() - 1}(gen_)];
}
return random;
}
const int random_length = 6;
const string tiny_url = "http://tinyurl.com/";
const string alphabet_ = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
default_random_engine gen_;
unordered_map<string, string> lookup_;
};
// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));