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
import java.io.*;
import java.util.*;
class GFG
{
public static void main(String []args)
{
String str = new String( "/a/./b/../../c/" );
String res = simplify(str);
System.out.println(res);
}
static String simplify(String A)
{
Stack<String> st = new Stack<String>();
String res = "" ;
res += "/" ;
int len_A = A.length();
for ( int i = 0 ; i < len_A; i++)
{
String dir = "" ;
while (i < len_A && A.charAt(i) == '/' )
i++;
while (i < len_A && A.charAt(i) != '/' )
{
dir += A.charAt(i);
i++;
}
if (dir.equals( ".." ) == true )
{
if (!st.empty())
st.pop();
}
else if (dir.equals( "." ) == true )
continue ;
else if (dir.length() != 0 )
st.push(dir);
}
Stack<String> st1 = new Stack<String>();
while (!st.empty())
{
st1.push(st.pop());
}
while (!st1.empty())
{
if (st1.size() != 1 )
res += (st1.pop() + "/" );
else
res += st1.pop();
}
return res;
}
}
|
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#
using System;
using System.Collections.Generic;
class GFG
{
public static void Main(String []args)
{
String str = ( "/a/./b/../../c/" );
String res = simplify(str);
Console.WriteLine(res);
}
static String simplify(String A)
{
Stack<String> st = new Stack<String>();
String res = "" ;
res += "/" ;
int len_A = A.Length;
for ( int i = 0; i < len_A; i++)
{
String dir = "" ;
while (i < len_A && A[i] == '/' )
i++;
while (i < len_A && A[i] != '/' )
{
dir += A[i];
i++;
}
if (dir.Equals( ".." ) == true )
{
if (st.Count!=0)
st.Pop();
}
else if (dir.Equals( "." ) == true )
continue ;
else if (dir.Length != 0)
st.Push(dir);
}
Stack<String> st1 = new Stack<String>();
while (st.Count!=0)
{
st1.Push(st.Pop());
}
while (st1.Count!=0)
{
if (st1.Count!= 1)
res += (st1.Pop() + "/" );
else
res += st1.Pop();
}
return res;
}
}
|
Javascript
<script>
function simplify(A)
{
let st = [];
let res = "" ;
res += "/" ;
let len_A = A.length;
for (let i = 0; i < len_A; i++)
{
let dir = "" ;
while (i < len_A && A[i] == '/ ')
i++;
// stores directory' s name("a ", " b " etc.)
// or commands(" . "/" .. ") into dir
while (i < len_A && A[i] != '/')
{
dir += A[i];
i++;
}
// if dir has " .. " just pop the topmost
// element if the Stack is not empty
// otherwise ignore.
if (dir == " .. ")
{
if (st.length!=0)
st.pop();
}
// if dir has " . " then simply continue
// with the process.
else if (dir == " . ")
continue;
// pushes if it encounters directory's
// name(" a ", " b ").
else if (dir.length != 0)
st.push(dir);
}
// a temporary Stack (st1) which will contain
// the reverse of original Stack(st).
let st1 = [];
while (st.length!=0)
{
st1.push(st[st.length - 1]);
st.pop();
}
// the st1 will contain the actual res.
while (st1.length!=0)
{
// if it's the last element no need
// to append " / "
if (st1.length!= 1)
{
res += (st1[st1.length - 1] + " / ");
st.pop();
}
else
{
res += st1[st1.length - 1];
st1.pop();
}
}
return res;
}
// absolute path which we have to simplify.
let str = (" /a/./b/../../c/");
let res = simplify(str);
document.write(res);
</script>
|
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;
class GFG {
static string simplify( string path)
{
List< string > v = new List< string >();
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.Count != 0)
v.RemoveAt(v.Count - 1);
}
else if (dir == "." || dir == "" ) {
}
else {
v.Add(dir);
}
}
foreach ( string i in v) {
ans += "/" + i;
}
if (ans == "" )
return "/" ;
return ans;
}
static void Main()
{
string str = "/a/./b/../../c/" ;
string res = simplify(str);
Console.Write(res);
}
}
|
Javascript
<script>
function simplify(path)
{
let v = [];
let n = path.length;
let ans = "" ;
for (let i = 0; i < n; i++) {
let dir = "" ;
while (i < n && path[i] != '/' ) {
dir += path[i];
i++;
}
if (dir == ".." ) {
if (v.length > 0)
v.pop();
}
else if (dir == "." || dir == "" ) {
}
else {
v.push(dir);
}
}
for (let i of v) {
ans += "/" + i;
}
if (ans == "" )
return "/" ;
return ans;
}
let Str = "/a/./b/../../c/" ;
let res = simplify(Str);
document.write(res);
</script>
|
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. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Nov, 2023
Like Article
Save Article