-
Notifications
You must be signed in to change notification settings - Fork 28
/
Encode_and_Decode_TinyURL.cpp
58 lines (47 loc) · 1.52 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
public:
unordered_map<string, string> url2Code;
unordered_map<string, string> code2Url;
string alphaNums = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
int ENCODE_LENGTH = 6;
string generateCode(string const& longUrl) {
string code = "";
for (int i = 0; i < ENCODE_LENGTH; i++) {
code += alphaNums[rand() % alphaNums.length()];
}
return code;
}
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
if (url2Code.find(longUrl) != url2Code.end()) {
return "http://tinyurl.com/" + url2Code[longUrl];
}
string code;
do {
code = generateCode(longUrl);
} while (code2Url.find(code) != code2Url.end());
code2Url[code] = longUrl;
url2Code[longUrl] = code;
return "http://tinyurl.com/" + code;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
string code = shortUrl.substr(shortUrl.length() - ENCODE_LENGTH, ENCODE_LENGTH);
return code2Url[code];
}
};
// joke
class Solution {
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
return longUrl;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return shortUrl;
}
};
// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));