Skip to content
Onsite

Simplify Path

MediumP2Coding

Given an absolute Unix-style path path, return its canonical form.

  • A single dot . means the current directory and is dropped.
  • Two dots .. mean the parent directory; at the root they have no effect.
  • Multiple consecutive slashes count as one slash.
  • Any other sequence of characters, including three or more dots such as ..., is an ordinary directory or file name.
  • The result starts with exactly one /, has no trailing / (except for the root itself), and contains no . or .. components.

Example 1

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

Example 2

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

Example 3

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

Constraints

  • 1 <= path.length <= 3000
  • path has letters, digits, '.', '/' and '_'
  • path is a valid absolute path starting with '/'

Hints

Run checks the 3 visible tests, Submit all 12.