Easy to follow Java solution using List (6ms)


  • 0
    N

    During the single pass of the split string array, construct path of file and compare with maxLen.

        public int lengthLongestPath(String input) {
            List<String> path = new ArrayList<String>();
            String[] elements = input.split("\n");
            int maxLen = 0;
            for (int i = 0; i < elements.length; i++) {
                String element = elements[i];
                int level = 0;
                while('\t' == element.charAt(0)) {      // num of \t identify level
                    element = element.substring(1);
                    level++;
                }
                if (path.size() > level)               // only keep directories up to my level
                    path = path.subList(0,level);
                path.add(element);
                if (element.indexOf(".")!=-1) {
                    String filePath = String.join("/",path);
                    if (filePath.length() > maxLen) maxLen = filePath.length();
                }
            }
            return maxLen;
        }
    

Log in to reply
 

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