Simplify Path
Last updated
Was this helpful?
Last updated
Was this helpful?
String
, Stack
Medium
Given anabsolute pathfor a file (Unix-style), simplify it. Or in other words, convert it to thecanonical path.
In a UNIX-style file system, a period.
refers to the current directory. Furthermore, a double period..
moves the directory up a level. For more information, see:
Note that the returned canonical path must always begin with a slash/
, and there must be only a single slash/
between two directory names. The last directory name (if it exists)must not end with a trailing/
. Also, the canonical path must be theshorteststring representing the absolute path.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Example 6:
The main idea is to push to the stack every valid file name (not in {"",".",".."}), popping only if there's smth to pop and we met "..". I don't feel like the code below needs any additional comments.
13 ms, faster than 73.84%
@shpolsky