388. Longest Absolute File Path

class Solution {
    public int lengthLongestPath(String input) {
        String [] arr=input.split("\n");
        int [] lens= new int[arr.length];
       
        int max=0;
        for(String s:arr){
            int lastBackT= s.lastIndexOf("\t");
            int level = lastBackT+1; 
            int parentLen = level==0? 0: lens[level-1]+1;
            int nameLen = s.length()-lastBackT-1;
            lens[level] = parentLen+nameLen;
            if(s.contains("."))
            {
                max = Math.max(max,lens[level]);
            }
        }
        
        return max;
    }
}

The first thought is DFS, however, we're able to deal it with array

上一篇:leetcode 3. Longest Substring Without Repeating Characters


下一篇:S - From S To T(有点难度的思维题(关于字符串配对字符))