std::hash C++ soln

  • 0

    Important to note, if some day the test cases increase and collision occurs, we can simply change the str.resize(size) to new size, right now it is 8.

    class Solution {
        // Encodes a URL to a shortened URL.
        unordered_map<string, string> umap;
        string encode(string longUrl) {
            auto hashx = std::hash<std::string>()(longUrl);
            string str = "";
            str = to_string(hashx);
            umap.emplace(str, longUrl);
            return "" + str;
        // Decodes a shortened URL to its original URL.
        string decode(string shortUrl) {
            shortUrl = shortUrl.substr(19,8);
            return umap[shortUrl];

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.