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
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.