yokostan / Leetcode-Solutions

Doing exercise on Leetcode. Carry on!
0 stars 3 forks source link

Leetcode #388. Longest Absolute File Path #295

Open yokostan opened 5 years ago

yokostan commented 5 years ago
class Solution {
    public int lengthLongestPath(String input) {
        if (input == null || input.length() == 0) return 0;
        Deque<Integer> stack = new ArrayDeque<>();
        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;
    }
}