# 388. Longest Absolute File Path

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/longest-absolute-file-path/description/

> 内容描述

```
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 directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 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 directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.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 is 32 (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, return 0.

Note:
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 path aaaaaaaaaaaaaaaaaaaaa/sth.png.
```

## 解题方案

> 思路 1
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******

我们首先观察到每个```文件夹```或者是```文件```前面都会有一个```'\n'```, 还有对应其层数个数的```'\t'```. 
- 所以首先根据```'\n'```分行，然后算出该```文件/文件夹```的层数```depth```
- 如果是```文件```，我们需要更新```maxlen```
- 如果是```文件夹```，我们需要更新该```depth```下的```pathlen```

### 程序变量解释

- ```maxlen``` 代表目前最大子串的长度
- ```pathlen``` 每一个```depth```下对应的```path```长度

#### 特别需要注意的是，```'\t'```的长度是1
有的人仍然会有疑问，每次碰到文件夹都直接更新```pathlen```会不会导致本来长的反而变得短了，但是我们可以看到字符串的排版格式，每层```path```都是严格有自己的分级的，
因此不会出现这样的问题。
例如:
- 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
```  
其最大长度是```32```, ```"dir/subdir2/subsubdir2/file2.ext"```

- 如果变成```"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir20\n\t\tsubsubdir2\n\t\t\tfile2.ext"```，

```
dir
    subdir1
        file1.ext
        subsubdir1
    subdir20
        subsubdir2
            file2.ext
```

最大长度就是```33```,
```"dir/subdir2/subsubdir20/file2.ext"```


- 如果变成
```"dir\n\tsubdir1000000000000\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"```

```
dir
    subdir10000000000000
        file1.ext
        subsubdir1
    subdir20
        subsubdir2
            file2.ext
```

最大长度就是```34```,```"dir/subdir10000000000000/file1.ext"```
         
beats 99.66%

```python
class Solution(object):
    def lengthLongestPath(self, input):
        """
        :type input: str
        :rtype: int
        """
        lap = 0
        depth_len = {0: 0}
        for line in input.splitlines():
            name = line.lstrip('\t')
            # 前面有几个'\t', depth就是几, 因为'\t'的长度为1
            depth = len(line) - len(name) 
            if '.' in name:
                lap = max(lap, depth_len[depth]+len(name))
            else:
                # 加1是为了加上一个path分隔符'/'的长度
                depth_len[depth+1] = depth_len[depth] + 1 + len(name) 
        return lap
```


