Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X

Simplify the Directory

Average time to solve is 22m
Contributed by
Asked in companies
FacebookVisaGoldman Sachs

Problem statement

You are given a path to a file/directory in Unix-style of length N, In a Unix-style file system, a dot(.) refers to the current directory. A double dot(..) refers to the previous directory in reference to the current directory. If there are multiple slashes between two directories you should consider it as a single slash.

Now, for a given directory path as a string, you are required to simplify the same and tell the final destination in the directory structure or the path.

The simplified path should always begin with a slash(/) and there must be a single slash between two directory names. There should not be a trailing slash.

Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 100
0 <= N <= 10^5
Where N is the length of the input path.

Time Limit : 1 sec
Sample Input 1:
Sample Output 1 :
Explanation for input 1:
Directory should always start with a slash and there should not be a trailing slash.
Sample Input 2:
Sample Output 2 :
Explanation for input 2:
For test case one, as double dot represents the previous directory so going one step back from the current directory(b) is a. 

For the second test case, at first we are at ‘a’, then we encounter a dot(.) then we move to ‘b’, then we have a double dot(..) so double dot means we have to go one step back, so now we will be at ‘a’, then again we have a double dot so going one step back again,  we are at empty directory then we encounter c, so the final answer would be /c.
Full screen