class Solution { public int lengthLongestPath(String input) { String [] arr=input.split(" "); int [] lens= new int[arr.length]; int max=0; for(String s:arr){ int lastBackT= s.lastIndexOf(" "); int level = lastBackT+1; int parentLen = level==0? 0: lens[level-1]+1; int nameLen = s.length()-lastBackT-1; lens[level] = parentLen+nameLen; if(s.contains(".")) { max = Math.max(max,lens[level]); } } return max; } }
The first thought is DFS, however, we're able to deal it with array