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.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.
C++
Java
Python
C#
JavaScript
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.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n).
Space Complexity: O(1).
| 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). |
Crawler Log Folder - Leetcode 1598 - Python • NeetCodeIO • 4,650 views views
Watch 9 more video solutions →Practice Crawler Log Folder with our built-in code editor and test cases.
Practice on FleetCode