Suppose we abstract our file system by a string in the following manner:
The string"dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
represents:
dir
subdir1
subdir2
file.ext
The directorydir
contains an empty sub-directorysubdir1
and a sub-directorysubdir2
containing a file file.ext.
The string
"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
represents:
dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
The directorydir
contains two sub-directoriessubdir1
andsubdir2
.subdir1
contains a file file1.ext and an empty second-level sub-directorysubsubdir1
.subdir2
contains a second-level sub-directorysubsubdir2
containing a filefile2.ext
.
We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is"dir/subdir2/subsubdir2/file2.ext"
, and its length is32
(not including the double quotes).
Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return0
.
Notice
- The name of a file contains at least a
.
and an extension. - The name of a directory or sub-directory will not contain a
.
. - Time complexity required:
O(n)
where n is the size of the input string. - Notice that
a/aa/aaa/file1.txt
is not the longest file path, if there is another pathaaaaaaaaaaaaaaaaaaaaa/sth.png
.
Have you met this question in a real interview?
Yes
Example
Give input ="dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
return
class Solution:
"""
@param input: an abstract file system
@return: return the length of the longest absolute path to file
"""
#need to do a dfs on the path, so file path is linked together if the later part is one level higher than the previous one
#we can first break path into lines so that we can read one line at a time
#to see if the level of the file we can compare the number of \t
#use a stack to preserve the previous path, and a value of length to keep how long is it so far
#we keep pop from the stack until the previous level is less than the current one
#if it is a file then we update answer
#If it is a folder then we push it to stack
def lengthLongestPath(self, input):
# write your code here
if not input or len(input) == 0:
return 0
lines = input.strip().split("\n")
print(lines)
ans = 0
stack = []
length = 0
level = -1
for line in lines:
is_word = "." in line
cur_level = line.count("\t")
cur_length = len(line) - cur_level
#initial loading
if len(stack) == 0:
if is_word:
ans = max(ans, length + cur_length)
else:
length += cur_length + 1
stack.append((cur_length, cur_level))
continue
#to find the largest level of previous path that's smaller than current
while len(stack) > 0 and cur_level <= stack[-1][1]:
length -= stack.pop()[0] + 1
print stack
#if it is file then update
if is_word:
ans = max(ans, length + cur_length)
#if it is not then push to stack
else:
length += cur_length + 1
stack.append((cur_length, cur_level))
return ans