1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { public int lengthLongestPath(String input) { int n = input.length(), pos = 0, ans = 0; Deque<Integer> stack = new ArrayDeque<>(); while (pos < n) { int depth = 1; while (pos < n && input.charAt(pos) == '\t') { ++pos; ++depth; } boolean isFile = false; int length = 0; while (pos < n && input.charAt(pos) != '\n') { if (input.charAt(pos) == '.') isFile = true; ++pos; ++length; } ++pos; while (stack.size() >= depth) stack.pop(); if (!stack.isEmpty()) length += stack.peek() + 1;
if (isFile) ans = Math.max(ans, length); else stack.push(length); } return ans; } }
|