3ms Java one pass solution


  • 0
    J
    public int lengthLongestPath(String input) {
            int len = input.length();
            int[] counts = new int[len / 2];
            int curLevel = 1;
            int count = 0;
            boolean isFile = false;
            int res = 0;
            for (int i = 0; i < len; i++) {
                char c = input.charAt(i);
                if (c == '\n') {
                    if (isFile) {
                        res = Math.max(res, counts[curLevel - 1] + curLevel - 1 + count);
                    } else {
                        counts[curLevel] = counts[curLevel - 1] + count;
                    }
                    count = 0;
                    curLevel = 1;
                    isFile = false;
                } else if (c == '\t') {
                    curLevel++;
                } else if (c == '.') {
                    isFile = true;
                    count++;
                } else {
                    count++;
                }
            }
            if (isFile) {
                res = Math.max(res, counts[curLevel - 1] + curLevel - 1 + count);
            }
            return res;
        }
    

Log in to reply
 

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