> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Encode and Decode TinyURL Python Solution

> Tested Python solution for LeetCode 535 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 535, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Design](/catalog/topics/design). [View on LeetCode](https://leetcode.com/problems/encode-and-decode-tinyurl/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 535   # by problem number
lcpy gen -s encode_and_decode_tinyurl   # by problem name
```

## Problem

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.

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

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 `Codec` class:

* `Codec()` 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.

### Examples

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

Explanation:
Codec codec = new Codec();
string tiny = codec.encode(url); // returns the encoded tiny url.
string ans = codec.decode(tiny); // returns the original url after decoding it.
```

### Constraints

* `1 <= url.length <= 10^4`
* `url` is guranteed to be a valid URL.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/encode_and_decode_tinyurl/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/encode_and_decode_tinyurl/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Codec:
    # Time: O(1) average per operation
    # Space: O(n) for n encoded URLs
    def __init__(self) -> None:
        self.url_map: dict[str, str] = {}
        self.encode_map: dict[str, str] = {}

    def encode(self, long_url: str) -> str:
        if long_url in self.encode_map:
            return self.encode_map[long_url]
        short_url = f"http://tinyurl.com/{len(self.url_map)}"
        self.url_map[short_url] = long_url
        self.encode_map[long_url] = short_url
        return short_url

    def decode(self, short_url: str) -> str:
        return self.url_map[short_url]
```

## Complexity

| Time | Space |
| - | - |
| O(1) average per operation | O(n) for n encoded URLs |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.