forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrawler-Log-Folder.cpp
More file actions
18 lines (18 loc) · 827 Bytes
/
Crawler-Log-Folder.cpp
File metadata and controls
18 lines (18 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int minOperations(vector<string>& logs) {
stack<string> s; //stack s is initialized and stack is empty when you are on main folder
for(int i=0;i<logs.size();i++){
if (logs[i]=="../" && !s.empty()){ //if string is "../" and stack is not empty then go back one folder
s.pop();
}
else if((logs[i]=="./") || (logs[i]=="../" && s.empty())) { //if string is "./" or stack is empty and string is "../" then do nothing
continue;
}
else { // otherwise push the new folder on top of stack
s.push(logs[i]);
}
}
return s.size(); //number of steps required to go back to main folder will be equal to the number of elements remaining in the stack
}
};