Simplify Directory Path

Medium

Given a string 'path' representing the absolute path of a file in a Unix-like file system, simplify it.

Note:

  • Absolute path will always start with a '/'.
  • A period '.' refers to the current directory.
  • A double period '..' refers to the parent directory.
  • Simplified path:
    • Starts with a '/'
    • Any two directories are separated by a '/'.
    • Does not end with a '/' unless it is the root path.
    • Should only contain directories from root path to target file/directory and does not contain '.' or '..'.
Examples
Absolute Path: "/home/"
Simplified Path: "/home"
Absolute Path: "/../"
Simplified Path: "/"
Absolute Path: "/a/./b/../../c/../d/"
Simplified Path: "/d"

Testing

Input Format

The first line contains an integer ‘T’, denoting the number of test cases.

For each test case the input contains a single line with a string denoting the absolute path.

Output Format

For each test case, a line containing a string denoting the simplified path.

Sample Input

3
/home/
/../
/a/./b/../../c/../d/

Expected Output

/home
/
/d

Constraints

1 <= T <= 100

1 <= n <= 3000

Valid characters in path: a-z, 0-9, '.', '/'

Editorial Link: Editorial