2Brute2Best
🔥0

Simplify Path

Medium·LeetCode #71 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given an absolute Unix-style file path path (it always starts with /). Rewrite it in its shortest, canonical form.

The rules of the path:

  • . means the current folder, so it changes nothing.
  • .. means the parent folder. At the root /, there is no parent, so you stay at the root.
  • Several slashes in a row (//, ///) count as one /.
  • Any other piece is a folder name, including things like ... or ..a.

The canonical path starts with a single /, has exactly one / between folder names, does not end with / (unless it is just /), and contains no . or .. pieces.

Input

A string path.

Output

The simplified canonical path as a string.

Example 1
Input: path = "/home/"
Output: "/home"

The trailing slash is dropped.

Example 2
Input: path = "/home//foo/"
Output: "/home/foo"

The double slash counts as one.

Example 3
Input: path = "/home/user/Documents/../Pictures"
Output: "/home/user/Pictures"

.. goes back up from Documents to user, then we enter Pictures.

Example 4
Input: path = "/../"
Output: "/"

The root has no parent, so .. keeps us at the root.

Example 5
Input: path = "/.../a/../b/c/../d/./"
Output: "/.../b/d"

... is an ordinary folder name. a is undone by the .. after it, c by the next .., and . changes nothing.

Constraints
  • • 1 ≤ path.length ≤ 3000
  • • path contains English letters, digits, '.', '/' and '_'
  • • path is a valid absolute Unix path
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“".." moves up one level”
→ It undoes the most recent folder still open → pop the top of a stack.
“a folder name moves down one level”
→ It opens a new level → push.
“"." and "//" change nothing”
→ Those pieces are skipped and never touch the stack.
03

Approach Complexity Ladder

Brute Force — rewrite the string until it stops changingO(N²)O(N)

Repeatedly collapse "//", remove "/./", and cancel "/name/../" pairs

Optimal — split on '/', then a folder stackO(N)O(N)

Skip "" and ".", pop on ".." if not empty, push any other name; join with '/'

Brute Force Intuition

Add a / at the end so every name is followed by /. Then keep applying the first rule that matches, searching from the start each time: // → /, /./ → /, a leading /../ → /, and /name/../ → / (where name is not ..). When no rule matches, drop the trailing /.

Recognition rule for this sub-pattern:

Simulate a process where new items interact with the latest survivors (collisions, path folders, min tracking).
Remember this · Stack
Trigger
Matching pairs, nested structure, undo, evaluating expressions, "the last one wins".
Think
Does each new element need to look at the most recent unresolved element?
Key question
What does an item on the stack represent, and when does it get resolved?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Stack translate into production architectures and low-level memory mechanics.