public int lengthLongestPath(String input){
  Deque<Integer> stack=new ArrayDeque<>();
  stack.push(0);
  String[] files=input.split("\n");
  int max=0;
  for (  String f : files) {
    int level=f.lastIndexOf("\t") + 1;
    while (stack.size() > level + 1) {
      stack.pop();
    }
    int len=stack.peek() + f.length() - level + 1;
    stack.push(len);
    if (f.contains(".")) {
      max=Math.max(max,len - 1);
    }
  }
  return max;
}

public int lengthLongestPath ( String input ) { Deque < Integer > stack = new ArrayDeque < > ( ) ; stack . push ( 0 ) ; String [ ] files = input . split ( "\n" ) ; int max = 0 ; for ( String f : files ) { int level = f . lastIndexOf ( "\t" ) + 1 ; while ( stack . size ( ) > level + 1 ) { stack . pop ( ) ; } int len = stack . peek ( ) + f . length ( ) - level + 1 ; stack . push ( len ) ; if ( f . contains ( "." ) ) { max = Math . max ( max , len - 1 ) ; } } return max ; }
stack . using stack to save previous length at each level . find current level by the last index of " \ t " in filename + 1 . compare current level with stack size , stack size should be one level larger than current level . calculate current length , with slash at the end . push this length into stack . if current name is a file , update max . the length here should minus 1 to remove the last " / " .
