Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int lengthLongestPath(String input) {
- String[] split = input.split("\\\n");
- Stack<Integer> stack = new Stack<>(); // contains string length
- int curLen = 0, maxLen = 0;
- for(String str : split)
- {
- int lvl = getLev(str);
- while(!stack.isEmpty() && stack.size() >= lvl) curLen -= stack.pop();
- if(stack.isEmpty() || stack.size() < lvl)
- {
- int len = getLen(str) + 1; // + 1 for '/'
- stack.push(len);
- curLen += len;
- }
- if(str.indexOf('.') >= 0) maxLen = Math.max(maxLen, curLen - 1);
- }
- return maxLen;
- }
- int getLen(String str)
- {
- int n = str.length();
- for(int i = 0; i < n; i++) if(str.charAt(i) != '\t') return str.substring(i).length();
- return n;
- }
- int getLev(String str)
- {
- int lv = 1;
- for(char ch : str.toCharArray())
- {
- if(ch != '\t') break;
- lv++;
- }
- return lv;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement