logo
Interview
    Interview Guide
    Coding Problems List
Sponsored: Coursera
Problems

Simplify Path

Problem

Given an absolute path for a file (Unix-style), simplify it.

Example

"/home/", => "/home"
"/a/./b/../../c/", => "/c"

Solution

What we need to do:

  • Remove . (current directory) and .. (parent directory).
  • Replace multiple consecutive slashes with a single slash
  • The path starts with a single slash /
  • No trailing slashes

To deal with .., it may be easier to use Linked List, if your chosen languages provides this.

To deal with /, we can simply split by / and re-join the string.

Online Judge