-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay6.cpp
More file actions
75 lines (61 loc) · 1.85 KB
/
Day6.cpp
File metadata and controls
75 lines (61 loc) · 1.85 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
This problem was asked by Microsoft.
Implement a URL shortener with the following methods:
shorten(url), which shortens the url into a six-character alphanumeric string, such as zLg6wl.
restore(short), which expands the shortened string into the original url. If no such shortened string exists, return null.
Hint: What if we enter the same URL twice?
*/
#include <bits/stdc++.h>
using namespace std;
class URLShortener
{
private:
unordered_map<string, string> urlMap;
unordered_map<string, string> reverseMap;
const string characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string generateShortUrl()
{
srand(time(0));
string shortUrl;
for (int i = 0; i < 6; ++i)
{
shortUrl += characters[rand() % characters.size()];
}
return shortUrl;
}
public:
string shorten(const string &url)
{
if (reverseMap.find(url) != reverseMap.end())
{
return reverseMap[url];
}
string shortUrl = generateShortUrl();
while (urlMap.find(shortUrl) != urlMap.end())
{
shortUrl = generateShortUrl();
}
urlMap[shortUrl] = url;
reverseMap[url] = shortUrl;
return shortUrl;
}
string restore(const string &shortUrl)
{
if (urlMap.find(shortUrl) != urlMap.end())
{
return urlMap[shortUrl];
}
return "";
}
};
int main()
{
URLShortener urlShortener;
string originalUrl = "https://github.com/blackphoenix42/Daily-Coding-Problem";
string shortUrl = urlShortener.shorten(originalUrl);
cout << "Original URL: " << originalUrl << endl;
cout << "Shortened URL: " << shortUrl << endl;
string restoredUrl = urlShortener.restore(shortUrl);
cout << "Restored URL: " << restoredUrl << endl;
return 0;
}