simple java solution


  • 0
    2
    public class Solution {
        public int lengthLongestPath(String input) {
            String[] token = input.split("\n");
            Stack<Integer> s = new Stack();
            int ret = 0;
            for (String t : token) {
                int depth = 0;
                while (t.length() > 1 && t.substring(0, 1).equals("\t")) {
                    t = t.substring(1);
                    depth++;
                }
                while (depth < s.size()) {
                    s.pop();
                }
                if (t.contains(".")) {
                    if (s.isEmpty()) {
                        ret = Math.max(ret, t.length());
                    }
                    else {
                        ret = Math.max(ret, s.peek() + t.length());
                    }
                }
                else {
                    if (s.isEmpty()) {
                        s.push(t.length() + 1);
                    }
                    else {
                        s.push(s.peek() + t.length() + 1);
                    }
                }
            }
            return ret;
        }
    }
    

Log in to reply
 

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