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.
Example 1:

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

Input: logs = ["d1/","d2/","./","d3/","../","d31/"] Output: 3
Example 3:
Input: logs = ["d1/","../","../","../"] Output: 0
Constraints:
1 <= logs.length <= 1032 <= logs[i].length <= 10logs[i] contains lowercase English letters, digits, '.', and '/'.logs[i] follows the format described in the statement.Problem Overview: You are given a list of folder navigation logs such as "../", "./", or "x/". Each log represents moving up a directory, staying in the current folder, or entering a subfolder. The goal is to compute the minimum number of operations needed to return to the main folder after processing all logs.
The commands behave exactly like terminal navigation. "../" moves one level up (unless you are already at the root), "./" keeps you in the same directory, and any other string ending with "/" means entering a new folder. The problem becomes a simple state tracking task where you simulate how deep you are in the folder structure.
Approach 1: Using Depth Counter (O(n) time, O(1) space)
This approach keeps a single integer variable representing the current folder depth. Iterate through the array of logs and update the depth based on each command. When encountering "../", decrease the depth only if it is greater than zero. Ignore "./" because it does not change the directory. For any other log (like "d1/"), increment the depth because you move into a subfolder.
The key insight is that the actual folder names do not matter. Only the direction of movement matters. Because the algorithm processes each log once and stores only an integer counter, it runs in O(n) time with O(1) extra space. This is the simplest and most efficient solution and is typically what interviewers expect.
Approach 2: Using Stack for Folder Simulation (O(n) time, O(n) space)
This method simulates the directory structure using a stack. Traverse the logs one by one. When the command is a folder name like "x/", push it onto the stack. When the command is "../", pop from the stack if it is not empty. Ignore "./" since it keeps you in the same folder.
After processing all logs, the number of elements remaining in the stack represents how many directories deep you are from the root. That value equals the minimum operations needed to return to the main folder. This approach models the navigation process explicitly and mirrors how real file systems manage paths.
The stack-based solution also runs in O(n) time because each log is processed once. However, it uses O(n) space in the worst case if every log moves into a new folder. The logic relies heavily on operations typical in string processing and stack manipulation.
Recommended for interviews: The depth counter solution is usually preferred. It shows that you recognized the problem only tracks folder depth rather than actual paths. The stack approach is still valuable because it demonstrates understanding of directory simulation and stack-based state management.
This approach involves maintaining a counter to keep track of the current folder depth. For each log operation, adjust the counter accordingly:
After processing all the logs, the counter will give the minimum operations needed to return to the main folder, as it represents the current depth.
The C implementation uses the strcmp function to compare strings for directory operations and adjusts a counter representing the current directory depth. The final value of the counter gives the required number of operations to get back to the main folder.
Time Complexity: O(n), where n is the number of logs.
Space Complexity: O(1), no additional space is required except for the depth counter.
This approach simulates navigating through folders using a stack. The stack keeps track of folder paths:
The stack's size at the end represents the current depth, which is the number of operations needed to return to the main folder.
The C solution mimics the behavior of a stack using an integer counter to track how many directories down the user is. It simulates pushing and popping with integer operations.
Time Complexity: O(n).
Space Complexity: O(1).
Python
Java
C++
Go
TypeScript
JavaScript
Rust
C
| Approach | Complexity |
|---|---|
| Using Depth Counter | Time Complexity: O(n), where n is the number of logs. |
| Using Stack for Folder Simulation | Time Complexity: O(n). |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Depth Counter | O(n) | O(1) | Best choice when only folder depth matters and memory should remain minimal |
| Stack Simulation | O(n) | O(n) | Useful when explicitly modeling directory navigation or when folder names must be tracked |
Crawler Log Folder - Leetcode 1598 - Python • NeetCodeIO • 5,615 views views
Watch 9 more video solutions →Practice Crawler Log Folder with our built-in code editor and test cases.
Practice on FleetCode