Logo
Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Solution: Simplify Path

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:

1. 
   Input: "/a//b////c/d//././/.."
   Output: "/a/b/c"
   
2. 
   Input: "/../"
   Output: "/"

3. 
   Input: "/home//foo/"
   Output: "/home/foo"

Constraints:

  • 1 <= path.length <= 3000
  • path consists of English letters, digits, period '.', slash '/' or '_'.
  • path is a valid absolute Unix path.

.....

.....

.....

Like the course? Get enrolled and start learning!