-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTiny_Url_System_Desing_V_Imp.cpp
More file actions
40 lines (30 loc) · 1.01 KB
/
Tiny_Url_System_Desing_V_Imp.cpp
File metadata and controls
40 lines (30 loc) · 1.01 KB
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
class Solution {
public:
unordered_map<string,string> short_to_long,long_to_short;
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
if(long_to_short.count(longUrl)) {
return long_to_short[longUrl];
}
string dict="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
string result="";
while(result.size()) {
for(int i=0;i<6;i++) {
result+=(dict[rand()%dict.size()]);
}
if(short_to_long.count(result)) {
result.clear();
}
}
short_to_long[result]=longUrl;
long_to_short[longUrl]=result;
return result;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return short_to_long[shortUrl];
}
};
// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));