엔지니어 게시판
LeetCode 솔루션 분류

535. Encode and Decode TinyURL

컨텐츠 정보

본문

[ Medium ] 535. Encode and Decode TinyURL


Note: This is a companion problem to the System Design problem: Design TinyURL.

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

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.

Implement the Solution class:

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

 

Example 1:

Input: url = "https://leetcode.com/problems/design-tinyurl"
Output: "https://leetcode.com/problems/design-tinyurl"

Explanation:
Solution obj = new Solution();
string tiny = obj.encode(url); // returns the encoded tiny url.
string ans = obj.decode(tiny); // returns the original url after deconding it.

 

Constraints:

  • 1 <= url.length <= 104
  • url is guranteed to be a valid URL.

관련자료

댓글 3

austin님의 댓글

  • 익명
  • 작성일
class Solution {
public:
    // Encodes a URL to a shortened URL.
    string encode(string longUrl) {
        m.emplace_back(longUrl);        
        return "http://tinyurl.com/" + to_string(m.size() - 1);
    }

    // Decodes a shortened URL to its original URL.
    string decode(string shortUrl) {
        return m[stoi(shortUrl.substr(19))];
    }
    
    vector<string> m;
};

bobkim님의 댓글

  • 익명
  • 작성일
Runtime: 4 ms, faster than 70.07% of C++ online submissions for Encode and Decode TinyURL.
Memory Usage: 6.7 MB, less than 71.13% of C++ online submissions for Encode and Decode TinyURL.
class Solution {
private:
    std::vector<std::string> vec;
public:

    // Encodes a URL to a shortened URL.
    string encode(string longUrl) {
        vec.push_back(longUrl);
        int surl = std::find(vec.begin(), vec.end(), longUrl)-vec.begin();
        return "https://tinyurl/"+std::to_string(surl);
    }

    // Decodes a shortened URL to its original URL.
    string decode(string shortUrl) {
        std::string surl = shortUrl.substr(shortUrl.find("https://tinyurl/")+16);
        int index=std::stoi(surl);        
        return vec[index];
    }
};

Coffee님의 댓글

  • 익명
  • 작성일
Runtime: 21 ms, faster than 6.12% of Java online submissions for Encode and Decode TinyURL.
Memory Usage: 43.4 MB, less than 53.27% of Java online submissions for Encode and Decode TinyURL.
public class Codec {

    HashMap<Integer, String> map = new HashMap<Integer, String>();
    
    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        map.put(longUrl.hashCode(), longUrl);
        return "http://tinyurl.com/" + longUrl.hashCode();
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        
        // extract hashCode
        String hashCode = shortUrl.replace("http://tinyurl.com/", "");
        return map.get(Integer.parseInt(hashCode));
        
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
전체 404 / 16 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 892 명
  • 오늘 방문자 5,236 명
  • 어제 방문자 6,975 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,537 명
알림 0