| 1 | class Solution { |
| 2 | public: |
| 3 | int minOperations(vector<string>& logs) { |
| 4 | stack<string> s; //stack s is initialized and stack is empty when you are on main folder |
| 5 | for(int i=0;i<logs.size();i++){ |
| 6 | if (logs[i]=="../" && !s.empty()){ //if string is "../" and stack is not empty then go back one folder |
| 7 | s.pop(); |
| 8 | } |
| 9 | else if((logs[i]=="./") || (logs[i]=="../" && s.empty())) { //if string is "./" or stack is empty and string is "../" then do nothing |
| 10 | continue; |
| 11 | } |
| 12 | else { // otherwise push the new folder on top of stack |
| 13 | s.push(logs[i]); |
| 14 | } |
| 15 | } |
| 16 | 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 |
| 17 | } |
| 18 | }; |