Watch 10 video solutions for Simplify Path, a medium level problem involving String, Stack. This walkthrough by NeetCode has 73,047 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path.
The rules of a Unix-style file system are as follows:
'.' represents the current directory.'..' represents the previous/parent directory.'//' and '///' are treated as a single slash '/'.'...' and '....' are valid directory or file names.The simplified canonical path should follow these rules:
'/'.'/'.'/', unless it is the root directory.'.' and '..') used to denote current or parent directories.Return the simplified canonical path.
Example 1:
Input: path = "/home/"
Output: "/home"
Explanation:
The trailing slash should be removed.
Example 2:
Input: path = "/home//foo/"
Output: "/home/foo"
Explanation:
Multiple consecutive slashes are replaced by a single one.
Example 3:
Input: path = "/home/user/Documents/../Pictures"
Output: "/home/user/Pictures"
Explanation:
A double period ".." refers to the directory up a level (the parent directory).
Example 4:
Input: path = "/../"
Output: "/"
Explanation:
Going one level up from the root directory is not possible.
Example 5:
Input: path = "/.../a/../b/c/../d/./"
Output: "/.../b/d"
Explanation:
"..." is a valid name for a directory in this problem.
Constraints:
1 <= path.length <= 3000path consists of English letters, digits, period '.', slash '/' or '_'.path is a valid absolute Unix path.Problem Overview: You receive a Unix-style absolute file path such as /a/./b/../../c/. The task is to convert it into its canonical form by removing redundant slashes, resolving . (current directory), and correctly handling .. (parent directory).
The challenge is correctly interpreting directory traversal rules while preserving valid folder names. This is primarily a string parsing problem combined with a structure that tracks directory history.
Approach 1: Using Stack Data Structure (O(n) time, O(n) space)
The most common solution uses a stack to simulate directory navigation. Split the path by the '/' delimiter and process each token sequentially. When you encounter a normal directory name, push it onto the stack. If the token is .., pop from the stack if it's not empty because it means moving to the parent directory. Ignore . and empty tokens produced by consecutive slashes. After processing all components, rebuild the canonical path by joining stack elements with '/'. Each path segment is processed once, giving O(n) time complexity where n is the length of the path string, and the stack may store up to O(n) characters in the worst case.
This approach mirrors how operating systems internally resolve paths. The stack cleanly models forward navigation (push) and backward navigation (pop). Because of its clarity and reliability, this is the solution most engineers write during interviews.
Approach 2: Optimized String Handling (O(n) time, O(n) space)
You can solve the problem without explicitly using a stack structure by processing the string and storing valid directory segments in a dynamic list or array. Iterate through the path while extracting directory names between slashes. For each extracted token, apply the same rules: skip empty strings and ., remove the last stored directory if the token is .., otherwise append the directory to the result list. After processing the full path, concatenate the stored segments with '/' to build the canonical result.
The difference from the stack approach is mostly conceptual. Instead of stack operations, you treat the result container as a dynamic list and manipulate indices. Time complexity remains O(n) because each character is scanned once, and space complexity stays O(n) for storing path segments. This version can feel more natural in languages where array manipulation is lightweight.
Recommended for interviews: The stack-based solution is what most interviewers expect. It clearly communicates the filesystem navigation logic and maps directly to the semantics of .. and directory traversal. Demonstrating the stack approach shows solid understanding of both string parsing and stack-based state tracking, which is exactly what this problem tests.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Stack Data Structure | O(n) | O(n) | Best general solution; clearly models directory traversal and is preferred in interviews |
| Optimized String Handling | O(n) | O(n) | Useful when implementing directly with arrays or lists without explicit stack structures |