class Solution { public int lengthLongestPath(String input) { if (input.length() == 0) return 0; String[] layers = input.split("\n"); int[] stack = new int[layers.length + 1]; int result = 0; for (String s : layers) { int level = s.lastIndexOf("\t") + 1, current = stack[level + 1] = stack[level] + s.length() - level + 1; // Find how many \t, which mean it belongs to which level. stack[level + 1] = stack[level] + s.length() - 1evel + 1 is calculate current level length by removing \t and update the prefix for next level if (s.contains(".")) result = Math.max(result, current - 1); // Check the last file. } return result; } }
浙公网安备 33010602011771号