Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Simplify Path

Medium
stackstring

Given an absolute Unix file path, return the simplified canonical path: it starts with a single /, has no trailing slash (unless it is the root), no . or .. segments, and no double slashes.

Input: one line, the path. Output: the canonical path.

Example 1
Input:
/home/
Output:
/home
Example 2
Input:
/../
Output:
/

You cannot go above the root.

Example 3
Input:
/home//foo/
Output:
/home/foo
Example 4
Input:
/a/./b/../../c/
Output:
/c

Constraints

  • 1 ≤ path.length ≤ 3000
  • Consists of letters, digits, '.', '/', '_'.
  • Always starts with '/'.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.