-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURLEncodeHashMap.cpp
More file actions
39 lines (31 loc) · 1016 Bytes
/
URLEncodeHashMap.cpp
File metadata and controls
39 lines (31 loc) · 1016 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
32
33
34
35
36
37
38
39
class Solution {
private:
unordered_map<string, string> urlMapping;
string base_url = "http://tinyurl.com/";
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
string url_postfix_hash;
for(int i = 0; i <= 6; i++) {
url_postfix_hash += randomCharGen();
}
string short_url = base_url + url_postfix_hash;
cout << "SHORT URL: " << short_url << endl;
urlMapping[short_url] = longUrl;
return short_url;
}
// Hash function
int randomCharGen() {
random_device seed;
mt19937 charGen(seed());
uniform_int_distribution<> ourDist(0,120);
return ourDist(charGen);
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return urlMapping[shortUrl];
}
};
// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));