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

# Crawler Log Folder Python Solution with Tests

> Tested Python solution for LeetCode 1598 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1598, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [Stack](/catalog/topics/stack). [View on LeetCode](https://leetcode.com/problems/crawler-log-folder/description/).

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

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

## Problem

The Leetcode file system keeps a log each time some user performs a change folder operation.

The operations are described below:

* `'../'` : Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder).
* `'./'` : Remain in the same folder.
* `'x/'` : Move to the child folder named x (This folder is guaranteed to always exist).

You are given a list of strings `logs` where `logs[i]` is the operation performed by the user at the ith step.

The file system starts in the main folder, then the operations in `logs` are performed.

Return the minimum number of operations needed to go back to the main folder after the change folder operations.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/09/09/sample_11_1957.png)

```
Input: logs = ["d1/","d2/","../","d21/","./"]
Output: 2
Explanation: Use this change folder operation "../" 2 times and go back to the main folder.
```

![Example 2](https://assets.leetcode.com/uploads/2020/09/09/sample_22_1957.png)

```
Input: logs = ["d1/","d2/","./","d3/","../","d31/"]
Output: 3
```

```
Input: logs = ["d1/","../","../","../"]
Output: 0
```

### Constraints

* 1 \<= logs.length \<= 10^3
* 2 \<= logs\[i].length \<= 10
* `logs[i]` contains lowercase English letters, digits, `'.'`, and `'/'`.
* `logs[i]` follows the format described in the statement.
* Folder names consist of lowercase English letters and digits.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def min_operations(self, logs: list[str]) -> int:
        depth = 0
        for op in logs:
            if op == "../":
                depth = max(0, depth - 1)
            elif op != "./":
                depth += 1
        return depth
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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