Kinda Poor Solution but with rutime 3ms and beats 74.57%


  • 0
    B

    Shift every char in longURL by 1 and revert it back later.

    class Solution {
    public:
    
        // Encodes a URL to a shortened URL.
        string encode(string longUrl) {
            std::string result = "http://tinyurl.com/";
            for(char ch : longUrl) {
                result.push_back(++ch);
            }
            return result;
        }
    
        // Decodes a shortened URL to its original URL.
        string decode(string shortUrl) {
            std::string result;
            for(size_t i = 19; i < shortUrl.size(); i++) {
                result.push_back(--shortUrl[i]);
            }
            return result;
        }
    };
    
    // Your Solution object will be instantiated and called as such:
    // Solution solution;
    // solution.decode(solution.encode(url));
    

Log in to reply
 

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