> ## 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 File System Python Solution with Tests

> Tested Python solution for LeetCode 1166 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 1166, [Medium](/catalog/medium). Topics: [Design](/catalog/topics/design), [Trie](/catalog/topics/trie), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/design-file-system/description/).

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

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

## Problem

You are asked to design a file system that allows you to create new paths and associate them with different values.

The format of a path is one or more concatenated strings of the form: `/` followed by one or more lowercase English letters. For example, `'/leetcode'` and `'/leetcode/problems'` are valid paths while an empty string `''` and `'/'` are not.

Implement the `FileSystem` class:

* `bool createPath(string path, int value)` Creates a new `path` and associates a `value` to it if possible and returns `true`. Returns `false` if the path **already exists** or its parent path **doesn't exist**.
* `int get(string path)` Returns the value associated with `path` or returns `-1` if the path doesn't exist.

### Examples

```
Input:
["FileSystem","createPath","get"]
[[],["/a",1],["/a"]]
Output:
[null,true,1]
Explanation:
FileSystem fileSystem = new FileSystem();

fileSystem.createPath("/a", 1); // return true
fileSystem.get("/a"); // return 1
```

```
Input:
["FileSystem","createPath","createPath","get","createPath","get"]
[[],["/leet",1],["/leet/code",2],["/leet/code"],["/c/d",1],["/c"]]
Output:
[null,true,true,2,false,-1]
Explanation:
FileSystem fileSystem = new FileSystem();

fileSystem.createPath("/leet", 1); // return true
fileSystem.createPath("/leet/code", 2); // return true
fileSystem.get("/leet/code"); // return 2
fileSystem.createPath("/c/d", 1); // return false because the parent path "/c" doesn't exist.
fileSystem.get("/c"); // return -1 because this path doesn't exist.
```

### Constraints

* `2 <= path.length <= 100`
* `1 <= value <= 10^9`
* Each `path` is **valid** and consists of lowercase English letters and `'/'`.
* At most `10^4` calls **in total** will be made to `createPath` and `get`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class FileSystem:
    # Time: create_path O(n), get O(n), where n is the number of path segments
    # Space: O(total number of segments across created paths)
    def __init__(self) -> None:
        self.paths: dict[str, int] = {}

    def create_path(self, path: str, value: int) -> bool:
        if path in self.paths:
            return False
        parent = path.rsplit("/", 1)[0]
        if parent and parent not in self.paths:
            return False
        self.paths[path] = value
        return True

    def get(self, path: str) -> int:
        return self.paths.get(path, -1)
```

## Complexity

| Time | Space |
| - | - |
| create\_path O(n), get O(n), where n is the number of path segments | O(total number of segments across created paths) |

## Tags

[NeetCode All](/catalog/neetcode).


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