-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode-and-decode-tinyurl.cpp
38 lines (33 loc) · 1.05 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
class Solution {
public:
Solution(){
dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
if(l2s.find(longUrl)!=l2s.end()) return l2s[longUrl];
string s="";
for(int i=0;i<6;i++){
s+=dict[rand()%dict.size()];
}
int idx=0;
while(l2s.find(s)!=l2s.end()){
s[idx]=dict[rand()%dict.size()];
idx=(idx+1)%6;
}
l2s[longUrl]=s;
s2l[s]=longUrl;
return "http://tinyurl.com/"+s;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
int pos=shortUrl.find_last_of('/');
string s=shortUrl.substr(pos+1);
return s2l[s];
}
unordered_map<string,string> l2s,s2l;
string dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
};
// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));