Unix Path Simplifier

HardAcc. 82.1%
+45 XP 20

Canonical Logic

Unix paths use .. to go up a level and . for current. A stack helps "track" current directory depth.

The Assignment

Your function receives path.

  1. Split the path by /.
  2. Iterate through parts:
    • If ".." and stack not empty, pop.
    • If valid name (not "" or "."), push.
  3. Print the joined path starting with /.

01EXAMPLE 1

Inputpath="/home//foo/"
Output/home/foo

Explanation: Double slash ignored.

Constraints

  • Final path must not have trailing slash.
Data StructuresStack
JavaScript
Loading...
1 Hidden

Input Arguments

path"/home/"

Expected Output

/home

Click RUN to test your solution