Practice/Meta/Leetcode 71. Simplify Path
CodingMust
You are given a string representing an absolute path in a Unix-style file system. Your task is to convert this path into its simplified canonical form.
In a Unix-style file system:
. refers to the current directory.. refers to the parent directory (one level up)// are treated as a single slash // (indicating the root)The canonical path must follow these rules:
/// (unless the path is just the root directory). or .. references..) keeps you at root. and ..)path consists of English letters, digits, period ., slash /, or underscore _path is a valid absolute Unix path (starts with /)Example 1:
Input: path = "/home/user/../documents" Output: "/home/documents" Explanation: The ".." moves up one directory from "user" to "home", then we continue to "documents".
Example 2:
Input: path = "/a/./b/../../c/" Output: "/c" Explanation: Step by step: start at root → a → stay at a (.) → b → back to a (..) → back to root (..) → c. The trailing slash is removed.
Example 3:
Input: path = "/../" Output: "/" Explanation: Attempting to go above the root directory keeps us at root. The result is just "/".
Example 4:
Input: path = "/home//documents///file.txt" Output: "/home/documents/file.txt" Explanation: Multiple consecutive slashes are collapsed into a single slash.