Encode and Decode TinyURL

    0

    0

    Mansimar Anand

    Encode and Decode TinyURL

    class Solution {
    private:
        string s;
        unordered_map<string, string> HashMap;
    public:
        Solution() {
            HashMap = {};
            for (int i = 0 ; i < 26 ; ++i) {
                if (i < 10) s += ('0' + i);
                s += string(1, 'a' + i) + string(1, 'A' + i);
            }
        }
        // Encodes a URL to a shortened URL.
        string encode(string longUrl) {
            string ans;
            for (int i = 0 ; i < 6 ; ++i) {
                ans += s[rand() % s.size()];
            }
            HashMap[ans] = longUrl;
            return ans;
        }
        // Decodes a shortened URL to its original URL.
        string decode(string shortUrl) {
            return HashMap[shortUrl];
        }
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.