AceYourInterviews
Checking sign-in…

48. Simplify Absolute Path

IntermediateLexical Unix component stack

Interpret repeated separators, current-directory components, and parent-directory components to produce a normalized absolute path. The result describes the same lexical location under these path rules.

A parent traversal cannot move above the root. This is lexical path processing, not filesystem access or symbolic-link resolution.

Example 1

Input

"/a//b/../c/./"

Output

"/a/c"

Explanation: Ignore empty/dot components and cancel b with its following parent traversal.

Example 2

Input

"/../../.../x"

Output

"/.../x"

Explanation: Root parents stay at root; ... is a literal directory name.

Constraints

  • 1 <= path.length <= 105
  • path begins with / and contains no NUL character. Slash is the only separator.
Hints
Hint 1

What does one parent component undo in the processed path?

Hint 2

Which names are special only by exact equality?

Hint 3

Build canonical components, clamp parents at root, and join once.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Canonical components survive only meaningful operations

"/a//b/../c/./"
"/a/c"