LeetCode Encode and Decode TinyURL

LeetCode Encode and Decode TinyURL

Note: This is a companion problem to the System Design problem: Design TinyURL.

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 the `encode` and `decode` methods for the TinyURL service. 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.

```class Solution {
private:
unsigned long long m_ullCnt;
unordered_map<unsigned long long, string> m_umHash;
public:
Solution() :m_ullCnt(0) {};

// Encodes a URL to a shortened URL.
string encode(string longUrl) {
m_umHash[m_ullCnt] = longUrl;
return "http://tinyurl.com/" + to_string(m_ullCnt++);
}

// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
int id = atoi(shortUrl.substr(19).c_str());
return m_umHash[id];
}
};
```

```class Solution {
private:
unordered_map<size_t, string> m_umHash;
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
hash<string> h;
m_umHash[h(longUrl)] = longUrl;
return "http://tinyurl.com/" + to_string(h(longUrl));
}

// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
size_t id = 0;
sscanf(shortUrl.substr(19).c_str(), "%zu", &id);
return m_umHash[id];
}
};
```