Advertisement

Encode and Decode TinyURL - LeetCode 535 Solution

Encode and Decode TinyURL - Complete Solution Guide

Encode and Decode TinyURL is LeetCode problem 535, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. I

Detailed Explanation

The problem asks us to design a system similar to TinyURL, a URL shortening service. We need to implement two functions: `encode(longUrl)` which takes a long URL as input and returns a shortened URL, and `decode(shortUrl)` which takes a shortened URL and returns the original long URL. There are no specific constraints on the encoding/decoding algorithm, but the solution must guarantee that the decoded URL is the same as the original URL.

Solution Approach

The provided solutions use a combination of a hash table and a counter. The `encode` function checks if the long URL already exists in the hash table. If it does, it returns the corresponding short URL. If not, it increments a counter, converts the counter value to a base-62 representation (using alphanumeric characters), stores the mapping in both `long_to_short` and `short_to_long` hash tables, and returns the new short URL. The `decode` function simply looks up the short URL in the `short_to_long` hash table and returns the corresponding long URL.

Step-by-Step Algorithm

  1. Step 1: Initialize two hash tables (dictionaries): `long_to_short` to store the mapping from long URLs to short keys and `short_to_long` to store the mapping from short keys to long URLs. Also, initialize a counter to generate unique short keys and define the set of characters (`alphabet`) to be used for base-N conversion.
  2. Step 2: In the `encode` function, check if the long URL already exists in the `long_to_short` hash table. If it does, return the corresponding short URL by concatenating the base URL with short key.
  3. Step 3: If the long URL is not in the hash table, increment the counter.
  4. Step 4: Convert the counter value to a base-N representation using the `alphabet`. This generates a unique short key.
  5. Step 5: Store the mapping between the long URL and the short key in both hash tables.
  6. Step 6: Return the newly generated short URL by concatenating the base URL with the short key.
  7. Step 7: In the `decode` function, extract the short key from the short URL by removing the base URL prefix.
  8. Step 8: Look up the short key in the `short_to_long` hash table and return the corresponding long URL.

Key Insights

  • Insight 1: We need a way to store the mapping between long URLs and short URLs. Hash tables (dictionaries in Python, HashMaps in Java/C++) are ideal for this.
  • Insight 2: The short URLs should be unique. A simple counter can be used to generate unique identifiers. These identifiers can be converted to a base-N representation (using alphanumeric characters) to create shorter, more user-friendly URLs.
  • Insight 3: The length of the short URL is related to the base (number of characters used) and the number of URLs we expect to store. A larger base allows for shorter URLs for the same number of URLs stored.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, String, Design, Hash Function.

Companies

Asked at: Shopify.