16 · Stacks & Monotonic Stack

#71 Simplify Path

Mediumsolve on LeetCode ↗

frequently reported atMetaMicrosoftAmazonGoogle

the problem

In plain words

Turn an absolute Unix path into its simplified canonical form: one '/' between names, no trailing '/', '.' means current directory, '..' means parent (staying at root if already there). Any other name, including '...', is a normal directory.

in path = "/home//foo/"

out "/home/foo"

in path = "/home/user/Documents/../Pictures"

out "/home/user/Pictures"

in path = "/../"

out "/"

in path = "/.../a/../b/c/../d/./"

out "/.../b/d"

  • 1 ≤ path.length ≤ 3000
  • Letters, digits, '.', '/', '_'
  • path is a valid absolute path

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #394
      Decode String
      Mediumin Stacks
    2. #844