-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encode and Decode TinyURL.cpp
35 lines (28 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
/*
Problem Title: Encode and Decode TinyURL
Problem URL: https://leetcode.com/problems/encode-and-decode-tinyurl/
Description: TinyURL is a URL shortening service where you enter a URL
such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.
Design a class to encode a URL and decode a tiny URL.
There is no restriction on how your encode/decode algorithm should work.
You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.
Difficulty: Medium
Language: C++
Category: Algorithms
*/
class Solution {
private:
map<int, string> mp;
int id = 0;
public:
// Encodes a URL to a shortened URL.
int encode(string longUrl) {
string sID = to_string(id);
mp[sID] = longUrl; id++;
return id;
}
// Decodes a shortened URL to its original URL.
string decode(int shortUrl) {
return mp[shortUrl];
}
};