Ctrl K

Steps Back to the Top Folder

Easy
A program moves between folders and records every move in the log moves. There are three kinds of commands: - "../" moves to the parent folder; if you are already in the top folder, you stay put; - "./" stays in the current folder; - "name/" moves into the child folder name (it always exists). The program starts in the top folder and then runs every command in the log in order. Return how many commands it takes afterwards to get back to the top folder.
Example 1
moves=['docs/', 'drafts/', './', 'old/', '../'] → 2

We go into docs, then drafts; "./" changes nothing; we enter old and step back out. We end up in docs/drafts, two steps below the top.

Example 2
moves=['../', 'src/', '../', '../'] → 0

You cannot go above the top folder, so the extra "../" do nothing. We finish at the top.

Constraints
  • 1 ≤ len(moves) ≤ 1000
  • 2 ≤ len(moves[i]) ≤ 10
  • Each command is "../", "./", or a folder name of lowercase English letters and digits followed by /
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSmallest Up-Down Sequence MediumGrading the Arithmetic Quiz HardBracket Balance EasyTwo Words, Closest Apart EasyNext Larger on the Right Easy
loading Python…
Input
moves=['docs/', 'drafts/', './', 'old/', '../']
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 77 tests on the server, hidden ones included.