Java solution


  • 0
    V
    class Solution {
        public int lengthLongestPath(String input) {
            String[] lines = input.split("\n");
            int[] stack = new int[lines.length];
            
            int maxLen = 0;
            for(String line : lines ) {
                int level = 0, lineLen = line.length();
                while (level < lineLen && line.charAt(level) == '\t') {
                    level++;
                }
                lineLen -= level;
                
                int cummulative = (level == 0) ? 0 : stack[level - 1];
                if (isFile(line)) {
                    maxLen = Math.max(maxLen, cummulative + lineLen + level);
                } else {
                    stack[level] = cummulative + lineLen;
                }
            }
            
            return maxLen;
        }
        
        boolean isFile(String s) {
            return s.indexOf(".") != -1;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.