Applied AI Engineer (LLM) coding learning path Level 2 This level · 9 of 31

Simplify Path

Medium Stack

Problem

Given an absolute Unix-style path, return the simplified canonical path. A canonical path starts with a slash, has no trailing slash unless it is the root, and contains no "." or ".." segments.

Original: LeetCode 71: Simplify Path.

Examples

Example 1

Input`path = "/home/"`
Output`"/home"`

Example 2

Input`path = "/home//foo/"`
Output`"/home/foo"`

Example 3

Input`path = "/home/user/Documents/../Pictures"`
Output`"/home/user/Pictures"`

Example 4

Input`path = "/../"`
Output`"/"`

Example 5

Input`path = "/.../a/../b/c/../d/./"`
Output`"/.../b/d"`

Constraints

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

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (2)
  1. LeetCode 71: Simplify Pathleetcode.com
  2. Java SE 17 API: ArrayDequedocs.oracle.com
Previous
Next