173. Simplify a File Path
Core1000 ms256 MBSolved by 0%
Read an absolute Unix-style path and print it in canonical form.
.means the current directory and is dropped..means go up one level; at the root it does nothing- Repeated slashes collapse into one
- The result starts with
/and, apart from the root itself, has no trailing slash
So /a/./b/../../c/ simplifies to /c.
Constraints - The path has between 1 and 3000 characters. - It always starts with `/`. - Names contain letters, digits, `.`, `_` and `-`.
Input
A single line containing an absolute Unix-style path, always starting with /.
Output
Print the canonical path. It always starts with / and, apart from the root itself, has no trailing slash.
Input/a/./b/../../c/
Output/c
Noteis the worked example, using every rule at once
Input/home/
Output/home
Notehas a trailing slash that must be dropped
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution