返回介绍

solution / 0500-0599 / 0535.Encode and Decode TinyURL / README_EN

发布于 2024-06-17 01:03:59 字数 5035 浏览 0 评论 0 收藏 0

535. Encode and Decode TinyURL

中文文档

Description

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 decoding it.

 

Constraints:

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

Solutions

Solution 1

class Codec:
  def __init__(self):
    self.m = defaultdict()
    self.idx = 0
    self.domain = 'https://tinyurl.com/'

  def encode(self, longUrl: str) -> str:
    """Encodes a URL to a shortened URL."""
    self.idx += 1
    self.m[str(self.idx)] = longUrl
    return f'{self.domain}{self.idx}'

  def decode(self, shortUrl: str) -> str:
    """Decodes a shortened URL to its original URL."""
    idx = shortUrl.split('/')[-1]
    return self.m[idx]


# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))
public class Codec {
  private Map<String, String> m = new HashMap<>();
  private int idx = 0;
  private String domain = "https://tinyurl.com/";

  // Encodes a URL to a shortened URL.
  public String encode(String longUrl) {
    String v = String.valueOf(++idx);
    m.put(v, longUrl);
    return domain + v;
  }

  // Decodes a shortened URL to its original URL.
  public String decode(String shortUrl) {
    int i = shortUrl.lastIndexOf('/') + 1;
    return m.get(shortUrl.substring(i));
  }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
class Solution {
public:
  // Encodes a URL to a shortened URL.
  string encode(string longUrl) {
    string v = to_string(++idx);
    m[v] = longUrl;
    return domain + v;
  }

  // Decodes a shortened URL to its original URL.
  string decode(string shortUrl) {
    int i = shortUrl.rfind('/') + 1;
    return m[shortUrl.substr(i, shortUrl.size() - i)];
  }

private:
  unordered_map<string, string> m;
  int idx = 0;
  string domain = "https://tinyurl.com/";
};

// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));
type Codec struct {
  m   map[int]string
  idx int
}

func Constructor() Codec {
  m := map[int]string{}
  return Codec{m, 0}
}

// Encodes a URL to a shortened URL.
func (this *Codec) encode(longUrl string) string {
  this.idx++
  this.m[this.idx] = longUrl
  return "https://tinyurl.com/" + strconv.Itoa(this.idx)
}

// Decodes a shortened URL to its original URL.
func (this *Codec) decode(shortUrl string) string {
  i := strings.LastIndexByte(shortUrl, '/')
  v, _ := strconv.Atoi(shortUrl[i+1:])
  return this.m[v]
}

/**
 * Your Codec object will be instantiated and called as such:
 * obj := Constructor();
 * url := obj.encode(longUrl);
 * ans := obj.decode(url);
 */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文