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.
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms
DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
13 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. How do
14 min read
Breadth First Search or BFS for a Graph
Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high. We sort the array using multiple passes. After the fi
8 min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Conditions to apply Binary Search Algorithm in a Data S
15+ min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Selection Sort
Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted. First we find the smallest element a
8 min read
Sorting Algorithms
A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read