Simple C++ Solution using Map

class Solution {
public:
unordered_map<string, string> m;
int nums=0;
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
nums++;
string addOn= to_string(nums);
string ans="http://tinyurl.com/";
ans+=addOn;
m[ans]=longUrl;
return ans;
}

// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
    return m[shortUrl];
}

};

Comments (0)