Minimum number of operations required to return to the main folder
Last Updated :
17 Jul, 2021
Given an array of strings arr[] representing the changed folder operations(Unix-style) performed on the file system. Initially, the file system opens in the main folder. The task is to find the minimum count of operations of the following three types to return to the main folder:
- “../”: Moves to the parent folder of the current folder. (If the current folder is the main folder, it remains in the same folder).
- “./”: Remains in the same folder.
- “F/”: Moves to the child folder named F.
Examples:
Input: arr[] = {“F1/”, “F2/”, “./”, “F3/”, “../”, “F31/”}
Output: 3
Explanation:
arr[0] = “F1/”. Move to the child folder named F1. Therefore, the path of the current directory is /F1.
arr[1] = “F2/”. Moves to the child’s folder, named F2. Therefore, the path of the current directory is /F1/F2
arr[2] = “./” . Remains in the current folder. Therefore, the path of the current directory is /F1/F2
arr[3] = “F3/”. Moves to the child’s folder, named F3. Therefore, the path of the current directory is /F1/F2/F3
arr[4] = “../”. Move to the parent folder of F3. Therefore, the path of the current directory is /F1/F2
arr[5] “F31/” . Move to the child’s folder, named F31. Therefore, the path of the current directory is /F1/F2/F31
Now, the “../” operation needs to be performed thrice to return to the main folder.
Therefore, the required output is 3.
Input: arr[] = {“F1/”, “../”, “../”}
Output: 0
Explanation:
arr[0] = “F1/”. Therefore, the path of the current directory is /F1.
arr[1] = “../”. Therefore, the path of the current directory is /
arr[2] = “../”. Therefore, the path of the current directory is /
Since the current path of the directory is already in the main folder, no operations are required. Therefore, the required output is 0.
Approach: The problem can be solved using Stack. Follow the steps below to solve the problem:
- Initialize a variable, say cntOp to store the count of minimum operations required to go back to the main folder.
- Create a Stack, say st to store the path of the current folder
- Traverse the array and check the following conditions:
- If arr[i] == “../” then pop the top element of the stack.
- If arr[i] == “F/” then push the value of arr[i] on the top of the stack.
- Finally, print the count of elements left onto the stack.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations(vector<string>& arr,
int N)
{
stack<string> st;
for ( int i = 0; i < N; i++) {
if (arr[i] == "../" &&
!st.empty()) {
st.pop();
}
else if (arr[i] != "./" ) {
st.push(arr[i]);
}
}
return st.size();
}
int main()
{
vector<string> arr
= { "F1/" , "F2/" , "./" ,
"F3/" , "../" , "F31/" };
int N = arr.size();
cout << minOperations(arr, N);
}
|
Java
import java.util.*;
class GFG{
static int minOperations(String []arr, int N)
{
Stack<String> st = new Stack<>();
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == "../" && !st.empty())
{
st.pop();
}
else if (arr[i] != "./" )
{
st.push(arr[i]);
}
}
return st.size();
}
public static void main(String args[])
{
String []arr = { "F1/" , "F2/" , "./" ,
"F3/" , "../" , "F31/" };
int N = arr.length;
System.out.print(minOperations(arr, N));
}
}
|
Python3
def minOperations(arr, N):
st = []
for i in range (N):
if (arr[i] = = "../" and len (st) ! = 0 ):
st.pop( - 1 )
elif (arr[i] ! = "./" ):
st.append(arr[i])
return len (st)
if __name__ = = '__main__' :
arr = [ "F1/" , "F2/" , "./" ,
"F3/" , "../" , "F31/" ]
N = len (arr)
print (minOperations(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int minOperations(String []arr,
int N)
{
Stack<String> st =
new Stack<String>();
for ( int i = 0; i < N; i++)
{
if (arr[i] == "../" &&
st.Count != 0)
{
st.Pop();
}
else if (arr[i] != "./" )
{
st.Push(arr[i]);
}
}
return st.Count;
}
public static void Main(String []args)
{
String []arr = { "F1/" , "F2/" , "./" ,
"F3/" , "../" , "F31/" };
int N = arr.Length;
Console.Write(minOperations(arr, N));
}
}
|
Javascript
<script>
function minOperations(arr, N)
{
st = [];
for ( var i = 0; i < N; i++) {
if (arr[i] == "../" && (st.length-1) !== 0) {
st.pop();
}
else if (arr[i] != "./" ) {
st.push(arr[i]);
}
}
return st.length;
}
var arr = [ "F1/" , "F2/" , "./" , "F3/" , "../" , "F31/" ];
var N = arr.length;
document.write( minOperations(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...