You are given a string, ‘S’, representing the path in the file system. Your task is to find the longest absolute file path.
In a file system, a directory doesn’t have an extension, whereas a file does have an extension. Eg: dir is a directory and file.txt is a file.
Example:
For the below file system, the string S will be:
S = “dir\n\tsubdir1\n\t\tfile1.txt\n\t\tfile2.txt\n\tfile.txt”

The above string will actually be represented as:
dir
subdir1
file1.txt
file2.txt
file.txt
Where character \n is the next line, and \t is the tab character i.e in the actual string instead of \t we will have a tab.
Every file has a unique absolute path.
Example:
file2.txt has an absolute path “dir/subdir1/file2.txt” and file.txt has path “dir/file.txt”.
The absolute file path length is the length of this string.
So absolute path length for file file2.txt will be the length of string “dir/subdir1/file2.txt” which is 21.
You need to print the longest of all such possible absolute file path lengths.
Note:If there is no file in the system, return 0.
The first line of the input contains the string ‘S’.
Note:
The first line of each input contains an integer that denotes the number of lines that will be given but this integer is not passed to the function because one of the aim of this question is to read input until EOF(End of the file).
Output Format:
The only line of output prints a single integer denoting the longest absolute file path.
0 <= |S| <= 5 * 10^3
Where |S| denotes the length of string S
Time limit: 1 sec
5
dir
-subdir1
--file1.txt
--file2.txt
-file.txt
21
There are three file paths possible
“dir/subdir1/file1.txt” of length 21
“dir/subdir1/file2.txt” of length 21
“dir/file.txt” of length 12
Therefore longest file path is of length 21.
4
dir
-subdir1
--subsubdir2
-subdir
0
Try to store the length of the previous folder in a data structure like stack.
The key idea in this approach is to traverse the string line by line and count the number of tabs to get the depth of the current directory/file while maintaining a stack that stores all previous directories needed to reach the current directory/file.
Algorithm:
O(|S|), where ‘|S|’ is the length of the string.
We are only iterating in the string so, the time complexity is O(|S|)
O(1)
We are only using the original string and not creating a new string.