Java solution 4ms


  • 0
    W
    public int lengthLongestPath(String input) {
        String[] iArr = input.split("\n");
        int len = iArr.length;
        int[] level = new int[len];
        int max = 0;
        
        for(int i = 0; i < len; i++) {
            char[] tmp = iArr[i].toCharArray();
            int levelt = 0;
            while(levelt < tmp.length && tmp[levelt] == '\t') levelt++;
            level[levelt] = levelt > 0 ? (level[levelt - 1] + 1) : 0;
            level[levelt] += tmp.length - levelt;
            if(iArr[i].contains(".")) {
                max = Math.max(max, level[levelt]);
            }
        }
        return max;
    }

Log in to reply
 

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