---
# System prepended metadata

title: 0388. Longest Absolute File Path
tags: [Leetcode, Medium, Stack]

---

# 0388. Longest Absolute File Path
###### tags: `Leetcode` `Medium` `Stack`
Link: https://leetcode.com/problems/longest-absolute-file-path/
## 思路
先把字符串根据"\n"分，然后根据"\t"的个数看它是在哪个level
stack里面存的是每个level对应的长度(是加了末尾'\'的长度)
## Code
```java=
class Solution {
    public int lengthLongestPath(String input) {
        Stack<Integer> stack = new Stack<>();
        stack.push(0);
        int maxLen = 0;
        for(String s:input.split("\n")){
            int lev = s.lastIndexOf("\t")+1;
            while(lev+1<stack.size()) stack.pop();
            int len = stack.peek()+s.length()-lev+1;
            stack.push(len);
            if(s.contains(".")){
                maxLen = Math.max(maxLen, len-1);
            }
        }
        return maxLen;
    }
}
```