Skip to main content
Back to problems
Leetcode
Medium
Strings
Stacks
Simulation
Simplify Path

Normalize an absolute Unix-style file path by resolving . , .. , and redundant slashes.

Acceptance 0%
Problem Statement

Given an absolute path in Unix-style format, return its simplified canonical form.

The path may contain:

  • Multiple consecutive slashes //
  • Current-directory markers .
  • Parent-directory markers ..

Rules:

  • . means stay in the current directory.
  • .. means move up one directory, if possible.
  • Multiple slashes should be treated as a single separator.
  • The simplified path must start with / and contain no trailing slash unless it is the root directory.

Write a program that converts the given path into its canonical form.

Input Format

  • A single string path representing an absolute Unix-style path.
  • The path begins with /.

Output Format

  • Return the simplified canonical path as a string.

Constraints

  • path is an absolute path.
  • The path may contain letters, digits, underscores, periods, and slashes.
  • Consecutive slashes should be treated as one separator.
  • The result must be a valid canonical absolute path.
Examples
Sample cases returned by the problem API.

Example 1

Input

path = "/home/"

Output

"/home"

Explanation

The trailing slash is removed in canonical form.

Example 2

Input

path = "/a/./b/../../c/"

Output

"/c"

Explanation

./ stays in the current directory, and each .. moves up one level.

Show 1 more example

Example 3

Input

path = "/../"

Output

"/"

Explanation

At the root directory, .. cannot move above /.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.