Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Simplify Path

XPChallenge Points: 20
levelLevel: Medium


You are given a full path that starts with /.
Change it to the shortest clean path using these rules:
• . means stay in the same folder.
• .. means go up one folder (if possible).
• Many slashes // or /// count as one slash.
• Names like ... or .... are just normal folder names.


The final clean path must:
1. Start with one slash /.
2. Have folder names separated by one slash.
3. Not end with a slash unless it is just /.
4. Have no . or .. left.
Return this clean path.

Example 1:

Input: "/data/"

Output: "/data"

Explanation:

Remove the last slash.

Example 2:

Input: "/data//logs/"

Output: "/data/logs"

Explanation:

Many slashes become one.

Example 3:

Input: "/music/rock/../pop"

Output: "/music/pop"

Explanation:

".." goes up one folder.

Example 4:

Input: "/../"

Output: "/"

Explanation:

Already at root, cannot go higher.

Example 5:

Input: "/abc/.../x/../y/./"

Output: "/abc/.../y"

Explanation:

"..." is a normal folder name.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result