Java Concise


  • 0
    public class Solution {
        public int lengthLongestPath(String input) {
            if (input == null || input.length() == 0) return 0;
            int res = 0;
            String[] str = input.split("\n");
            Map<Integer, Integer> map = new HashMap<>();
            map.put(-1, 0);
            for (int i = 0; i < str.length; i++) {
                String curr = str[i];
                int level = curr.lastIndexOf('\t') + 1; // for dir: level = 0; lastIndexOf
                if (curr.indexOf('.') != -1) {
                    res = Math.max(res, map.get(level - 1) + curr.length() - level);
                } else {
                    map.put(level, map.get(level - 1) + curr.length() - level + 1); // plus last level
                }
            }
            return res;
        }
    }
    

Log in to reply
 

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