Longest Absolute File Path - Teeeeeebag/LeetCode GitHub Wiki

Google

  1. String.split() accepts regex not a plain string
  2. "\t" is of length 1 not 2
  3. it the max length of a file not any director so update maxLen when a name has "." in it
  4. the path contains "/" which is not in the input string, should add this to maxLen
public class Solution {
    public int lengthLongestPath(String input) {
        if (input.length() == 0){ 
            return 0;
        }   
        String[] substrs = input.split("\\\n");
        int crtLen = 0, maxLen = 0;
        Stack<Integer> st = new Stack<>();
        for (int i=0; i<substrs.length; ++i){
            int depth = 0;
            while(depth < substrs[i].length() && substrs[i].charAt(depth) == '\t' ){
                depth++;
            }   
            int nameLen = substrs[i].length() - depth;
            while(!st.isEmpty() && st.size() > depth){
                crtLen = crtLen - st.pop();
            }   
            st.push(nameLen);
            crtLen += nameLen;
            if (substrs[i].indexOf(".")!=-1)
                maxLen = Math.max(maxLen, crtLen + st.size()-1);
        }   
        return maxLen;
         
    }   
}
⚠️ **GitHub.com Fallback** ⚠️