tutorialcup
Crawler Log Folder Leetcode Solution
Problem Statement
In this problem, we are keep tracking of our position in a folder system. We are initially at the root folder or the main folder of this system.
We have basically 3 kind of commands here. The commands are in the form of string in which each string means a command.
- "../" means go to the parent folder of the current folder (if already in the root folder then don't go anywhere).
- "./" stay in the current folder.
- "x/" go into the child folder of the current folder which has name x.
We are given a sequence of commands which is a combination of above 3 commands. We have to find out the minimum number of operation to go back to the root folder after preforming the given commands.
e.g.
logs =
We are at the depth 2 from the root folder. Hence, our answer will be 2.
Example
logs =
2
Explanation:
Use this change folder operation "../" 2 times and go back to the main folder.
logs =
3
Approach 1 (Using Stack)
We can use a stack of file names. Initially our stack is empty. Whenever we have to move to a subfolder then the name of the subfolder is pushed into the stack. Thus, the folder in which we are currently present will always remain on the top of the stack.
So, whenever we encounter "../", means we have to come out of the current folder (i.e. move to parent folder), then we will check if the current folder is root. If no, then we will pop the top element from stack.
And of course whenever we will encounter a "./",
www.tutorialcup.com/leetcode-solutions/crawler-log-folder...
Crawler Log Folder Leetcode Solution
Problem Statement
In this problem, we are keep tracking of our position in a folder system. We are initially at the root folder or the main folder of this system.
We have basically 3 kind of commands here. The commands are in the form of string in which each string means a command.
- "../" means go to the parent folder of the current folder (if already in the root folder then don't go anywhere).
- "./" stay in the current folder.
- "x/" go into the child folder of the current folder which has name x.
We are given a sequence of commands which is a combination of above 3 commands. We have to find out the minimum number of operation to go back to the root folder after preforming the given commands.
e.g.
logs =
We are at the depth 2 from the root folder. Hence, our answer will be 2.
Example
logs =
2
Explanation:
Use this change folder operation "../" 2 times and go back to the main folder.
logs =
3
Approach 1 (Using Stack)
We can use a stack of file names. Initially our stack is empty. Whenever we have to move to a subfolder then the name of the subfolder is pushed into the stack. Thus, the folder in which we are currently present will always remain on the top of the stack.
So, whenever we encounter "../", means we have to come out of the current folder (i.e. move to parent folder), then we will check if the current folder is root. If no, then we will pop the top element from stack.
And of course whenever we will encounter a "./",
www.tutorialcup.com/leetcode-solutions/crawler-log-folder...