
Problem 6: Simplify Path (medium)
Problem Statement
Given an absolute file path in a Unix-style file system, simplify it by converting ".." to the previous directory and removing any "." or multiple slashes. The resulting string should represent the shortest absolute path.
Examples
Example 1
- Input: path = "/a//b////c/d//././/.."
- Expected Output: "/a/b/c"
- Explanation:
- Convert multiple slashes (
//) into single slashes (/). "."refers to the current directory and is ignored.".."moves up one directory, so"d"is removed.- The simplified path is
"/a/b/c".
- Convert multiple slashes (
Example 2
- Input: path = "/../"
- Expected Output: "/"
- Explanation:
".."moves up one directory, but we are already at the root ("/"), so nothing happens.- The final simplified path remains
"/".
Example 3
- Input: path = "/home//foo/"
- Expected Output: "/home/foo"
- Explanation:
- Convert multiple slashes (
//) into single slashes (/). - The final simplified path is
"/home/foo".
- Convert multiple slashes (
Constraints:
1 <= path.length <= 3000- path consists of English letters, digits, period '.', slash '/' or '_'.
- path is a valid absolute Unix path.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory