question: 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 ; }
target:  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 "/".