Design a URL shortener.
The system should support creating a short URL for a given long URL, resolving a short code back to the original long URL, deactivating an existing short URL, and reading the details of a short URL.
Each created short URL must have a unique
shortCode. A short code can point to only one long URL at a time.
A short URL can be either active or inactive:
- When active, resolving the short code should return the original long URL.
- When inactive, resolving the short code should return an empty string.
Method Signatures
URLShortener(List<String> supportedDomains)
- Initializes the URL shortener with supported domains.
String createShortUrl(String shortCode, String longUrl, String domain)
- shortCode forms the short url. shortCode needs to be unique globally
e.g. abc123 under sho.rt and abc123 under go.io both can't exist
- Returns
domain/shortCode if successful, else empty string.
String resolveUrl(String shortCode)
- Returns long URL if active, else empty string.
boolean deactivateShortUrl(String shortCode)
- Returns true if successfully deactivated. False if shortCode doesn't exists or url was already deactivated.
- You can not reuse/recreate a deactivated shortCode.
List<String> getShortUrlDetails(String shortCode)
- Returns details list or empty list if shortCode is not found.
- returns ["SHORT_CODE:shortCode","LONG_URL:[longUrl]","DOMAIN:[domain]","SHORT_URL:[domain/shortCode]","STATUS:ACTIVE|INACTIVE"]
Constraints
1 ≤ supportedDomains.size() ≤ 100
1 ≤ shortCode.length() ≤ 100
1 ≤ longUrl.length() ≤ 2000
At most 10^5 calls
Examples
Example 1
URLShortener(["sho.rt","go.io"]) -> constructor
createShortUrl(shortCode="abc123", longUrl="https://example.com/products/1", domain="sho.rt") -> "sho.rt/abc123"
resolveUrl(shortCode="abc123") -> "https://example.com/products/1"
getShortUrlDetails(shortCode="abc123") -> ["SHORT_CODE:abc123","LONG_URL:https://example.com/products/1","DOMAIN:sho.rt","SHORT_URL:sho.rt/abc123","STATUS:ACTIVE"]
Example 2
URLShortener(["sho.rt"]) -> constructor
createShortUrl(shortCode="news", longUrl="https://news.example.com/article", domain="sho.rt") -> "sho.rt/news"
createShortUrl(shortCode="news", longUrl="https://another.example.com/page", domain="sho.rt") -> ""
resolveUrl(shortCode="news") -> "https://news.example.com/article"
Example 3
URLShortener(["sho.rt","go.io"]) -> constructor
createShortUrl(shortCode="offer2026", longUrl="https://shop.example.com/offers/summer", domain="go.io") -> "go.io/offer2026"
deactivateShortUrl(shortCode="offer2026") -> true
resolveUrl(shortCode="offer2026") -> ""
getShortUrlDetails(shortCode="offer2026") -> ["SHORT_CODE:offer2026","LONG_URL:https://shop.example.com/offers/summer","DOMAIN:go.io","SHORT_URL:go.io/offer2026","STATUS:INACTIVE"]
Example 4
URLShortener(["sho.rt"]) -> constructor
createShortUrl(shortCode="docs", longUrl="https://docs.example.com/start", domain="tiny.cc") -> ""
resolveUrl(shortCode="docs") -> ""
getShortUrlDetails(shortCode="docs") -> []
deactivateShortUrl(shortCode="docs") -> false