Encode and Decode TinyURL LeetCode solution in C++

1
class Solution {
2
private:
3
unordered_map<string, string> hash;
4
string server = "http://tinyurl.com/";
5
string all = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
6

7
public:
8
// Encodes a URL to a shortened URL.
9
string encode(string longUrl) {
10
srand(time(NULL));
11
string add = "";
12
int ran = rand() % 10;
13
int strsize = all.size();
14
while (ran) {
15
int index = rand() % strsize;
16
add += all[index];
17
ran--;
18
}
19

20
hash[(server + add)] = longUrl;
21

22
return (server + add);
23
}
24

25
// Decodes a shortened URL to its original URL.
26
string decode(string shortUrl) {
27
return hash[shortUrl];
28
}
29
};
30

31
// Your Solution object will be instantiated and called as such:
32
// Solution solution;
33
// solution.decode(solution.encode(url));

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0