A file system keeps a log each time a 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 step i.
The file system starts in the main folder, then the operations in logs are performed in order.
Return the minimum number of operations needed to go back to the main folder after the change folder operations.
Function Signature
def min_operations(logs: list[str]) -> int:
Constraints
-
1 <= len(logs) <= 10^3
-
2 <= len(logs[i]) <= 10
-
logs[i]
contains lowercase English letters, digits,
'.'
and
'/'
.
-
logs[i]
follows the format described above.
-
Folder names consist of lowercase English letters and digits.
Examples
Input: logs = ["d1/", "d2/", "../", "d21/", "./"]
Output: 2
After the operations the user is in d1/d21, two levels below the main folder, so the operation "../" is needed 2 times.
Input: logs = ["d1/", "d2/", "./", "d3/", "../", "d31/"]
Output: 3
The user ends in d1/d2/d31, three levels below the main folder, so 3 "../" operations are required.
Input: logs = ["d1/", "../", "../", "../"]
Output: 0
The first "../" returns to the main folder, and the next two leave the user there, so no operation is needed.