-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path535.cpp
More file actions
31 lines (28 loc) · 871 Bytes
/
535.cpp
File metadata and controls
31 lines (28 loc) · 871 Bytes
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
class Solution {
#define ABC "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrtuvwxyz"
#define ABC_SIZE 62
#define PREFIX "http://tinyurl.com/"
#define LENGTH 7
unordered_map<string, string> ump;
int index = 0;
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
string st(LENGTH,0);
int n = index++;
for(int i=LENGTH-1; i>=0; i--){
st[i] = ABC[n%ABC_SIZE];
n /= ABC_SIZE;
}
ump[st] = longUrl;
return PREFIX+st;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
string code = shortUrl.substr(shortUrl.size()-LENGTH);
return ump[code];
}
};
// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));