Simplify Path
In Unix/Linux, file paths use "/" to separate directories. Example: "/home/user/docs" points to the "docs" folder. Sometimes paths have extra slashes: "/home//user/" Your task: Clean up a path by removing duplicate/trailing slashes. Example: "/home//user/" → "/home/user" Example: "///a//b/" → "/a/b"
I split the path on slashes and keep a stack of folders. A double dot pops one; a single dot or empty piece is skipped.
Examples
You write
simplify_path(path: str)
Example 1
- Input
- "/home/"
- returns
- "/home"
Example 2
- Input
- "/home//foo/"
- returns
- "/home/foo"
The pattern behind it
Stack
I reach for it when
- Things must close in the reverse order they opened.
- You need the nearest earlier or later item that is bigger or smaller.
Say this before you type. Say what each item on the stack is waiting for, and what lets you pop it.
All 17 Stack problemsStrings
I reach for it when
- The input is text and the answer is a count, a substring or a new string.
- You need to compare, reverse or rebuild characters.
Say this before you type. Say which array pattern the string is hiding before you touch a character.
All 51 Strings problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Each locked part opens in the editor once the part before it passes.