← 返回 uber 的题目列表Design a TinyURL service (encode/decode)
类型:online_judge
Problem: Design a TinyURL service (encode/decode)
Design a URL shortening service that can encode a long URL into a short URL and decode it back.
Implement:
class Codec:
def encode(self, longUrl: str) -> str:
"""Encodes a URL to a shortened URL."""
def decode(self, shortUrl: str) -> str:
"""Decodes a shortened URL to its original URL."""
Requirements / Constraints
Repeated encode(longUrl) may:
return the same short URL (idempotent), or
return different short URLs (many-to-one allowed), but decode must work.
Avoid collisions; keep codes reasonably short.
Must satisfy decode(encode(x)) == x.
Example
codec = Codec()
short = codec.encode("https://example.com/a/b?c=1")
decode(short) -> "https://example.com/a/b?c=1"
Example
Input
encode https://example.com/a
then decode
Output
decode returns https://example.com/a