Crawler Log Folder — LeetCode 1598 Python Solution

EasyStackArrayString
Problem
#1598
Pattern
Stack
Reading time
2 min

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

Example

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

Python solution

Python
class Solution:
    def minOperations(self, logs: List[str]) -> int:
        ans = 0
        for v in logs:
            if v == "../":
                ans = max(0, ans - 1)
            elif v[0] != ".":
                ans += 1
        return ans

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Pattern: Stack

When the most recent unresolved thing is the one that matters, use a stack. LeetCode 1598. Crawler Log Folder is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Stack.

The stack guide has the Python template for the pattern and the 194 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1598. Crawler Log Folder?
LeetCode 1598. Crawler Log Folder is rated Easy on LeetCode.
What is the time complexity of LeetCode 1598. Crawler Log Folder?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 1598. Crawler Log Folder?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 1598. Crawler Log Folder cover?
LeetCode 1598. Crawler Log Folder is tagged Stack, Array and String on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview