Java AC solution 6ms


  • 0
    Y

    public class Solution {
    public int lengthLongestPath(String input) {
    if(input == null || input.length() == 0){
    return 0;
    }
    Stack<Wrap> stack = new Stack<Wrap>();
    String[] piles = input.split("\n");
    int max = 0;
    for(int i = 0 ; i < piles.length; i ++){
    String name = piles[i];
    int numOfT = 0;
    int j;
    for( j = 0 ; j < name.length(); j ++){
    String check = name.substring(j, j + 1);
    if(check.equals("\t")){
    numOfT ++;
    }else{
    break;
    }
    }
    while(!stack.isEmpty() && stack.peek().numOfT + 1 != numOfT){
    stack.pop();
    }
    int parentLength = stack.isEmpty() ? 0 : stack.peek().length;
    int numOfTParent = stack.isEmpty() ? 0 : stack.peek().numOfT;
    if(!name.contains(".")) {
    Wrap tmp = new Wrap(name.substring(j), parentLength, numOfT);
    stack.push(tmp);
    }else{
    max = Math.max(max, parentLength + name.length() - numOfT + stack.size());
    }
    }
    return max;
    }
    }
    class Wrap{
    String name;
    int length;
    int numOfT;
    public Wrap(String name, int parentLength, int numOfT){
    this.name = name;
    this.length = parentLength + name.length();
    this.numOfT = numOfT;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.