<blockquote>Note: This is a companion problem to the <a href="https://leetcode.com/discuss/interview-question/system-design/" target="_blank">System Design</a> problem: <a href="https://leetcode.com/discuss/interview-question/124658/Design-a-URL-Shortener-(-TinyURL-)-System/" target="_blank">Design TinyURL</a>.</blockquote>

<p>TinyURL is a URL shortening service where you enter a URL such as <code>https://leetcode.com/problems/design-tinyurl</code> and it returns a short URL such as <code>http://tinyurl.com/4e9iAk</code>. Design a class to encode a URL and decode a tiny URL.</p>

<p>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.</p>

<p>Implement the <code>Solution</code> class:</p>

<ul>
	<li><code>Solution()</code> Initializes the object of the system.</li>
	<li><code>String encode(String longUrl)</code> Returns a tiny URL for the given <code>longUrl</code>.</li>
	<li><code>String decode(String shortUrl)</code> Returns the original long URL for the given <code>shortUrl</code>. It is guaranteed that the given <code>shortUrl</code> was encoded by the same object.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> url = &quot;https://leetcode.com/problems/design-tinyurl&quot;
<strong>Output:</strong> &quot;https://leetcode.com/problems/design-tinyurl&quot;

<strong>Explanation:</strong>
Solution obj = new Solution();
string tiny = obj.encode(url); // returns the encoded tiny url.
string ans = obj.decode(tiny); // returns the original url after decoding it.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= url.length &lt;= 10<sup>4</sup></code></li>
	<li><code>url</code> is guranteed to be a valid URL.</li>
</ul>

#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;

class Solution {
private:
    unordered_map<string, string> longToShort;
    unordered_map<string, string> shortToLong;
    int counter;
    const string baseUrl = "http://tinyurl.com/";

    // 将计数器转换为一个短字符串
    string encodeIntToShortUrl(int counter) {
        string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        string shortUrl;
        while (counter > 0) {
            shortUrl.push_back(chars[counter % 62]);
            counter /= 62;
        }
        return shortUrl;
    }

public:
    Solution() : counter(1) {}

    // 将长URL编码为短URL
    string encode(string longUrl) {
        // 如果长URL已经被编码过，直接返回对应的短URL
        if (longToShort.find(longUrl) != longToShort.end()) {
            return baseUrl + longToShort[longUrl];
        }

        // 生成新的短URL
        string shortUrl;
        do {
            shortUrl = encodeIntToShortUrl(counter);
            counter++;
        } while (shortToLong.find(shortUrl) != shortToLong.end());

        // 存储映射关系
        longToShort[longUrl] = shortUrl;
        shortToLong[shortUrl] = longUrl;

        cout<<longUrl<<" "<<(baseUrl + shortUrl)<<endl;
        return baseUrl + shortUrl;
    }

    // 将短URL解码为长URL
    string decode(string shortUrl) {
        // 提取短URL部分
        string key = shortUrl.substr(baseUrl.length());
        return shortToLong[key];
    }
};


11000