Crawler Log Folder LeetCode solution in Python

1
class Solution:
2
def minOperations(self, logs: List[str]) -> int:
3
m = "../"
4
r = "./"
5
# create an empty stack
6
stk = []
7
# iterate through the list
8
for i in logs:
9
# if Move to the parent folder (../) operator occurs and stack is not empty, pop element from stack
10
if i == m:
11
if len(stk) > 0:
12
stk.pop()
13
# else if Remain in the same folder (./) operator occurs, do nothing and move to next element in list
14
elif i == r:
15
continue
16
# else add element to the stack
17
else:
18
stk.append(i)
19
# now return the size of the stack which would be the minimum number of operations needed to go back to the main folder
20
return len(stk)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0