- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a logs where we have path to enter into folders, there may be different symbols like −

"../" : Move to the parent folder from current one. (If we are at main folder, do not change location).

"./" : Remain in the current folder.

"x/" : Move to the child folder named x.

From the logs we have to find minimum number of operations needed to come back from last folder where we stop to the main folder.

So, if the input is like logs = ["Dir1/","Dir2/","../","Dir2/","Dir3/","./"], then the output will be 3

from the image we can see we have to step back thrice to reach home.

To solve this, we will follow these steps −

stk := a new list

for each item i in logs, do

if i is same as "../" and size of stk > 0, then

delete last element from stk

otherwise when i is not same as "./" and i is not same as "../", then

insert i at the end of stk

otherwise,

go for next iteration

return number of items in the stk

Let us see the following implementation to get better understanding −

def solve(logs): stk = [] for i in logs: if i == "../" and len(stk) > 0: stk.pop() elif i != "./" and i != "../": stk.append(i) else: continue return len(stk) logs = ["Dir1/","Dir2/","../","Dir2/","Dir3/","./"] print(solve(logs))

["Dir1/","Dir2/","../","Dir2/","Dir3/","./"]

3

- Related Questions & Answers
- Program to find minimum jumps to reach home in Python
- Program to find minimum swaps needed to group all 1s together in Python
- Program to find minimum element addition needed to get target sum in Python
- Program to find out the minimum rotations needed to maximize the profit from a Ferris wheel in Python
- Program to find minimum number of rocketships needed for rescue in Python
- Program to find minimum costs needed to fill fruits in optimized way in Python
- Program to find minimum amount needed to be paid all good performers in Python
- Program to find minimum operations needed to make two arrays sum equal in Python
- Program to count minimum deletions needed to make character frequencies unique in Python
- Find the minimum number of moves needed to move from one cell of matrix to another in Python
- Program to check minimum number of characters needed to make string palindrome in Python
- Find minimum operations needed to make an Array beautiful in C++
- Program to find minimum cost to cut a stick in Python
- Program to find number of coins needed to make the changes in Python
- Program to find how many swaps needed to sort an array in Python

Advertisements