Simplify the directory path (Unix like)
Last Updated :
02 Nov, 2023
Given an absolute path for a file (Unix-style), simplify it. Note that absolute path always begin with ‘/’ ( root directory ), a dot in path represent current directory and double dot represents parent directory.
Examples:
"/a/./" --> means stay at the current directory 'a'
"/a/b/.." --> means jump to the parent directory
from 'b' to 'a'
"////" --> consecutive multiple '/' are a valid
path, they are equivalent to single "/".
Input : /home/
Output : /home
Input : /a/./b/../../c/
Output : /c
Input : /a/..
Output:/
Input : /a/../
Output : /
Input : /../../../../../a
Output : /a
Input : /a/./b/./c/./d/
Output : /a/b/c/d
Input : /a/../.././../../.
Output:/
Input : /a//b//c//////d
Output : /a/b/c/d
Note: The given input will always have a valid absolute path.
Approach 1: By looking at examples we can see that the above simplification process just behaves like a stack. Whenever we encounter any file’s name, we simply push it into the stack. when we come across ” . ” we do nothing. When we find “..” in our path, we simply pop the topmost element as we have to jump back to parent’s directory.
When we see multiple “////” we just ignore them as they are equivalent to one single “/”. After iterating through the whole string the elements remaining in the stack is our simplified absolute path. We have to create another stack to reverse the elements stored inside the original stack and then store the result inside a string.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
string simplify(string A)
{
stack<string> st;
string dir;
string res;
res.append( "/" );
int len_A = A.length();
for ( int i = 0; i < len_A; i++) {
dir.clear();
while (A[i] == '/' )
i++;
while (i < len_A && A[i] != '/' ) {
dir.push_back(A[i]);
i++;
}
if (dir.compare( ".." ) == 0) {
if (!st.empty())
st.pop();
}
else if (dir.compare( "." ) == 0)
continue ;
else if (dir.length() != 0)
st.push(dir);
}
stack<string> st1;
while (!st.empty()) {
st1.push(st.top());
st.pop();
}
while (!st1.empty()) {
string temp = st1.top();
if (st1.size() != 1)
res.append(temp + "/" );
else
res.append(temp);
st1.pop();
}
return res;
}
int main()
{
string str( "/a/./b/../../c/" );
string res = simplify(str);
cout << res;
return 0;
}
|
Java
Python3
def simplify(A):
st = []
dir = ""
res = ""
res + = "/"
len_A = len (A)
i = 0
while i < len_A:
dir_str = ""
while (i < len_A and A[i] = = '/' ):
i + = 1
while (i < len_A and A[i] ! = '/' ):
dir_str + = A[i]
i + = 1
if dir_str = = ".." :
if len (st):
st.pop()
elif dir_str = = '.' :
continue
elif len (dir_str) > 0 :
st.append(dir_str)
i + = 1
st1 = []
while len (st):
st1.append(st[ - 1 ])
st.pop()
while len (st1):
temp = st1[ - 1 ]
if ( len (st1) ! = 1 ):
res + = (temp + "/" )
else :
res + = temp
st1.pop()
return res
string = "/a/./b/../../c/"
res = simplify(string)
print (res)
|
C#
Javascript
<div id= "highlighter_882263" class= "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class= "code" ><div class= "container" ><div class= "line number1 index0 alt2" ><code class= "plain" ><script></code></div><div class= "line number2 index1 alt1" ><code class= "undefined spaces" > </code><code class= "comments" >
|
Time Complexity: O(length of string).
Approach 2:
- In approach 1, the directories so formed, are first pushed into the stack and then the stack is reversed to form the canonical path.
- The only optimization here is to reduce the number of stack operations and this can be done by using vectors in place of a stack.
- Push and pop operations can be done in vector using push_back() and pop_back() functions respectively and the canonical path can be generated by simply traversing the vector from left to right.
Below is the implementation of approach 1 using vectors.
C++
#include <bits/stdc++.h>
using namespace std;
string simplify(string path)
{
vector<string> v;
int n = path.length();
string ans;
for ( int i = 0; i < n; i++) {
string dir = "" ;
while (i < n && path[i] != '/' ) {
dir += path[i];
i++;
}
if (dir == ".." ) {
if (!v.empty())
v.pop_back();
}
else if (dir == "." || dir == "" ) {
}
else {
v.push_back(dir);
}
}
for ( auto i : v) {
ans += "/" + i;
}
if (ans == "" )
return "/" ;
return ans;
}
int main()
{
string str( "/a/./b/../../c/" );
string res = simplify(str);
cout << res;
return 0;
}
|
Java
import java.util.*;
public class Main
{
static String simplify(String path)
{
Vector<String> v = new Vector<String>();
int n = path.length();
String ans = "" ;
for ( int i = 0 ; i < n; i++) {
String dir = "" ;
while (i < n && path.charAt(i) != '/' ) {
dir += path.charAt(i);
i++;
}
if (dir.equals( ".." )) {
if (v.size() != 0 )
{
v.remove(v.size() - 1 );
}
}
else if (dir.equals( "." ) || dir.equals( "" )) {
}
else {
v.add(dir);
}
}
for (String i : v) {
ans += "/" + i;
}
if (ans == "" )
return "/" ;
return ans;
}
public static void main(String[] args) {
String str = "/a/./b/../../c/" ;
String res = simplify(str);
System.out.print(res);
}
}
|
Python3
def simplify(path):
v = []
n = len (path)
ans = ""
for i in range (n):
Dir = ""
while (i < n and path[i] ! = '/' ):
Dir + = path[i]
i + = 1
if ( Dir = = ".." ) :
if ( len (v) > 0 ):
v.pop()
elif ( Dir = = "." or Dir = = ""):
continue
else :
v.append( Dir )
for i in v:
ans + = "/" + i
if (ans = = ""):
return "/"
return ans
Str = "/a/./b/../../c/"
res = simplify( Str )
print (res)
|
C#
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
public static string SimplifyPath( string path)
{
Stack< string > stack = new Stack< string >();
string [] tokens = path.Split( '/' );
foreach ( string token in tokens)
{
if (token == ".." ) {
if (stack.Count > 0) {
stack.Pop();
}
}
else if (! string .IsNullOrEmpty(token)
&& token != "." ) {
stack.Push(token);
}
}
string finalPath = "" ;
if (stack.Count == 0) {
finalPath = "/" ;
}
else {
string [] directories = stack.ToArray();
Array.Reverse(directories);
finalPath = "/" + string .Join( "/" , directories);
}
return finalPath;
}
static void Main()
{
string str = "/a/./b/../../c/" ;
string res = SimplifyPath(str);
Console.WriteLine(res);
}
}
|
Javascript
function simplifyPath(path) {
const stack = [];
const tokens = path.split( '/' );
for (const token of tokens) {
if (token === ".." ) {
if (stack.length > 0) {
stack.pop();
}
} else if (token && token !== "." ) {
stack.push(token);
}
}
let finalPath = "/" ;
if (stack.length > 0) {
finalPath += stack.join( '/' );
}
return finalPath;
}
const str = "/a/./b/../../c/" ;
const res = simplifyPath(str);
console.log(res);
|
Time Complexity: O(length of string).
Using Queue:
- If there are two dots then if size of deque is greater than 0 than remove one directory.
- If there is only one dot then ignore it.
- If there are more than two dots than consider it as directory name and put it into deque.
- Ignore all slashes and add in front at the end while popping out directories from deque.
C++
#include <iostream>
#include <deque>
#include <sstream>
#include <vector>
using namespace std;
string simplifyPath(string path) {
deque<string> stack;
istringstream iss(path);
string token;
while (getline(iss, token, '/' )) {
if (token == ".." ) {
if (!stack.empty()) {
stack.pop_back();
}
} else if (!token.empty() && token != "." ) {
stack.push_back(token);
}
}
string finalPath;
if (stack.empty()) {
finalPath += "/" ;
} else {
for ( const string& dir : stack) {
finalPath += "/" ;
finalPath += dir;
}
}
return finalPath;
}
int main() {
string str = "/a/./b/../../c/" ;
string res = simplifyPath(str);
cout << res;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static String simplify(String path) {
Deque<String> stack = new ArrayDeque<>();
String[] dirs = path.split( "/" );
for (String dir : dirs){
if (!dir.equals( "" ) && !dir.equals( "." )){
if (!dir.equals( ".." )) stack.add(dir);
else if (!stack.isEmpty()) stack.pollLast();
}
}
String finalPath = "" ;
if (stack.isEmpty()){
finalPath += "/" ;
} else {
for (String str : stack){
finalPath += "/" ;
finalPath += str;
}
}
return finalPath;
}
public static void main(String[] args) {
String str = "/a/./b/../../c/" ;
String res = simplify(str);
System.out.print(res);
}
}
|
Python
def simplifyPath(path):
stack = []
tokens = path.split( '/' )
for token in tokens:
if token = = ".." :
if stack:
stack.pop()
elif token and token ! = "." :
stack.append(token)
finalPath = ""
if not stack:
finalPath + = "/"
else :
finalPath + = "/" + "/" .join(stack)
return finalPath
if __name__ = = "__main__" :
path = "/a/./b/../../c/"
result = simplifyPath(path)
print (result)
|
C#
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
public static string SimplifyPath( string path)
{
Stack< string > stack = new Stack< string >();
string [] tokens = path.Split( '/' );
foreach ( string token in tokens)
{
if (token == ".." ) {
if (stack.Count > 0) {
stack.Pop();
}
}
else if (! string .IsNullOrEmpty(token)
&& token != "." ) {
stack.Push(token);
}
}
string finalPath = "" ;
if (stack.Count == 0) {
finalPath = "/" ;
}
else {
string [] directories = stack.ToArray();
Array.Reverse(directories);
finalPath = "/" + string .Join( "/" , directories);
}
return finalPath;
}
static void Main()
{
string str = "/a/./b/../../c/" ;
string res = SimplifyPath(str);
Console.WriteLine(res);
}
}
|
Javascript
function simplifyPath(path) {
const stack = [];
const tokens = path.split( '/' );
for (const token of tokens) {
if (token === ".." ) {
if (stack.length > 0) {
stack.pop();
}
} else if (token && token !== "." ) {
stack.push(token);
}
}
let finalPath = "/" ;
if (stack.length > 0) {
finalPath += stack.join( '/' );
}
return finalPath;
}
const str = "/a/./b/../../c/" ;
const res = simplifyPath(str);
console.log(res);
|
Time Complexity: O(length of the string).
Space Complexity: O(length of string).
This article is contributed by arshpreet soodan.
Share your thoughts in the comments
Please Login to comment...