ExamAdda Logo

Simplify Path

Easy

Given a string path representing an absolute Unix-style file path, simplify it to its canonical form.

In a Unix-style path:

  • . represents the current directory.
  • .. represents the parent directory.
  • Multiple consecutive slashes are treated as a single slash.
  • Any sequence of characters other than /, ., and .. represents a directory name.

Return the simplified canonical path.

Example 1

Input

path = "/home/"

Output

"/home"

Explanation

The trailing slash does not affect the canonical path, so the simplified path is /home.

Example 2

Input

path = "/home//foo/../bar/"

Output

"/home/bar"

Explanation

The consecutive slashes are treated as one slash, and .. moves from foo back to home. Therefore, the canonical path becomes /home/bar.

Constraints

1 <= path.length <= 3000
path consists of English letters
digits
period '.'
slash '/' or underscore '_'
path is a valid absolute Unix path

Hints:

Hint 1

Split the path using '/' and process each directory one by one.

Hint 2

Use a stack to store valid directory names. Ignore '.' and empty parts, and pop the last directory when '..' is encountered.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 18, 2026

Last updated

Aug 18, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output