606. Crawler Log Folder

EasyStackStack

Starting in the main folder, process a list of change-folder logs: '../' moves to the parent (staying if already at the main folder), './' stays, and any other 'x/' moves into a child. Return the minimum number of steps needed to return to the main folder afterward, i.e. the final depth. The input is JSON {logs}.

Input: JSON {logs}.

Output: Integer — the final folder depth.

Examples

Example 1
Input: {"logs":["d1/","d2/","../","d21/","./"]}
Output: 2
Explanation: Depth ends at 2.
Example 2
Input: {"logs":["../","../","../"]}
Output: 0
Explanation: Cannot go above main.
Example 3
Input: {"logs":["./"]}
Output: 0
Explanation: Stay in main.

Constraints

Asked by

Meta
Solve this problem in the editor →