Tap here to switch tabs
Problem
Ask
Submissions

Problem: Simplify Path

med
30 min
Understand how to transform Unix-style absolute paths into their simplified canonical forms using stacks. Explore handling special directory symbols like '.' and '..', managing multiple slashes, and ensuring proper path formatting. Practice implementing a solution that processes paths correctly according to Unix file system rules.

Statement

Given an absolute path for a Unix-style file system (always beginning with '/'), transform it into its simplified canonical form.

The Unix-style file system follows these rules:

  • A single period '.' represents the current directory.

  • A double period '..' represents the parent directory.

  • Multiple consecutive slashes (e.g., '//' or '///') are treated as a single slash '/'.

  • Any sequence of periods that does not match the above rules is treated as a valid directory or file name (e.g., '...' and '....' are valid names).

The resulting canonical path must satisfy the following:

  • It must begin with a single slash '/'.

  • Directories must be separated by exactly one slash '/'.

  • It must not end with a trailing slash '/', unless it is the root directory.

  • It must not contain any '.' or '..' components used to denote current or parent directories.

Return the simplified canonical path.

Constraints:

  • 11 \leq path.length 3000\leq 3000

  • path consists of English letters, digits, period '.', slash '/', or underscore '_'

  • path is a valid absolute Unix path

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Simplify Path

med
30 min
Understand how to transform Unix-style absolute paths into their simplified canonical forms using stacks. Explore handling special directory symbols like '.' and '..', managing multiple slashes, and ensuring proper path formatting. Practice implementing a solution that processes paths correctly according to Unix file system rules.

Statement

Given an absolute path for a Unix-style file system (always beginning with '/'), transform it into its simplified canonical form.

The Unix-style file system follows these rules:

  • A single period '.' represents the current directory.

  • A double period '..' represents the parent directory.

  • Multiple consecutive slashes (e.g., '//' or '///') are treated as a single slash '/'.

  • Any sequence of periods that does not match the above rules is treated as a valid directory or file name (e.g., '...' and '....' are valid names).

The resulting canonical path must satisfy the following:

  • It must begin with a single slash '/'.

  • Directories must be separated by exactly one slash '/'.

  • It must not end with a trailing slash '/', unless it is the root directory.

  • It must not contain any '.' or '..' components used to denote current or parent directories.

Return the simplified canonical path.

Constraints:

  • 11 \leq path.length 3000\leq 3000

  • path consists of English letters, digits, period '.', slash '/', or underscore '_'

  • path is a valid absolute Unix path