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

# Design Log Storage System Python Solution

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

LeetCode 635, [Medium](/catalog/medium). Topics: [Design](/catalog/topics/design), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Ordered Set](/catalog/topics/ordered-set). [View on LeetCode](https://leetcode.com/problems/design-log-storage-system/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 635   # by problem number
lcpy gen -s design_log_storage_system   # by problem name
```

## Problem

You are given several logs, where each log contains a unique ID and timestamp. Timestamp is a string that has the following format: `Year:Month:Day:Hour:Minute:Second`, for example, `2017:01:01:23:59:59`. All domains are zero-padded decimal numbers.

Implement the `LogSystem` class:

* `LogSystem()` Initializes the `LogSystem` object.
* `void put(int id, string timestamp)` Stores the given log `(id, timestamp)` in your storage system.
* `int[] retrieve(string start, string end, string granularity)` Returns the IDs of the logs whose timestamps are within the range from `start` to `end` inclusive. `start` and `end` all have the same format as `timestamp`, and `granularity` means how precise the range should be (i.e. to the exact `Day`, `Minute`, etc.).

### Examples

```
Input
["LogSystem", "put", "put", "put", "retrieve", "retrieve"]
[[1, "2017:01:01:23:59:59"], ...]
Output
[null, null, null, null, [3, 2, 1], [2, 1]]

Explanation
LogSystem logSystem = new LogSystem();
logSystem.put(1, "2017:01:01:23:59:59");
logSystem.put(2, "2017:01:01:22:59:59");
log.put(3, "2016:01:01:00:00:00");
logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year"); // return [3,2,1], all logs between 2016 and 2017.
logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour"); // return [2,1]
```

### Constraints

* `1 <= id <= 500`
* `2000 <= Year <= 2017`
* `1 <= Month <= 12`
* `1 <= Day <= 31`
* `0 <= Hour <= 23`
* `0 <= Minute, Second <= 59`
* `granularity` is one of the values `["Year", "Month", "Day", "Hour", "Minute", "Second"]`.
* At most `500` calls will be made to `put` and `retrieve`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class LogSystem:
    # Time: retrieve O(n), put O(1)
    # Space: O(n)
    def __init__(self) -> None:
        self.logs: list[tuple[int, str]] = []
        self.gran_len = {
            "Year": 4,
            "Month": 7,
            "Day": 10,
            "Hour": 13,
            "Minute": 16,
            "Second": 19,
        }

    def put(self, log_id: int, timestamp: str) -> None:
        self.logs.append((log_id, timestamp))

    def retrieve(self, start: str, end: str, granularity: str) -> list[int]:
        size = self.gran_len[granularity]
        lo = start[:size]
        hi = end[:size]
        return [log_id for log_id, ts in self.logs if lo <= ts[:size] <= hi]
```

## Complexity

| Time | Space |
| - | - |
| retrieve O(n), put O(1) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


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