Header Ad

Leetcode Longest Absolute File Path problem solution

In this Leetcode Longest Absolute File Path problem solution, you have given a string input representing the file system in the explained format, return the length of the longest absolute path to a file in the abstracted file system. If there is no file in the system, return 0.

Leetcode Longest Absolute File Path problem solution


Problem solution in Python.

class Solution:
    def lengthLongestPath(self, input: str) -> int:
        import re
        if '.' not in input:
            return 0
        dirs=input.split('\n')
        fli=[]
        for i in range(len(dirs)):
            if '.' in dirs[i]:
                fli.append(i)
        ans=[]
        for i in fli:
            cnt=dirs[i].count('\t')
            q=re.sub('\t','',dirs[i])
            cnt-=1
            for j in range(i,-1,-1):
                if cnt==-1:
                    break
                if dirs[j].count('\t')==cnt:
                    q=re.sub('\t','',dirs[j])+'/'+q
                    cnt-=1
            ans.append(len(q))
        return max(ans)



Problem solution in Java.

public int lengthLongestPath(String input) {
    int longest = 0;
    String[] lines = input.split("\n");
    int[] lens = new int[lines.length+1];
    for(String line: lines) {
        String[] subs = line.split("\t");
        String cur = subs[subs.length-1];
        int len = lens[subs.length-1] + cur.length() + 1;
        if(cur.contains(".")) longest = Math.max(longest, len-1);
        else lens[subs.length] = len;
    }
    return longest;
}


Problem solution in C++.

class Solution {
public:
  bool delimiter(const char* const p) {
    return *p == '\n' || *p == '\t';
  }

  int lengthLongestPath(string input) {
    std::stack<int> lengths;
    int total_length = 0;
    
    const char* p = &input[0];
    const char* const end = &input[0] + input.size();
    int best = 0;
    
    std::string name;
    int depth;
    
    while (p != end && !delimiter(p)) {
        name += *p;
        ++p;
    }
    
    if (name.find('.') != std::string::npos) {
        best = name.size();
    }
    lengths.push(name.size());
    total_length += name.size();
    name = "";

    while (p != end) {
        depth = parseDelimiter(&p, end);
        while (p != end && !delimiter(p)) {
            name += *p;
            ++p;
        }
        while (depth <= lengths.size()) {
            total_length -= lengths.top();
            lengths.pop();
        }
        lengths.push(name.size());
        total_length += name.size();
        if (name.find('.') != std::string::npos) {
            best = std::max(best, total_length + depth - 1);
        }
        name = "";
    }
    return best;
  }

  int parseDelimiter(const char** pp, const char* end) {
    int count = 0;
    while (*pp != end && delimiter(*pp)) {
        ++(*pp);
        ++count;
    }
    return count;
  }
};


Problem solution in C.

int lengthLongestPath(char* input) {
    int stack[256] = {0}; 
    int stack_idx = 0; 
    
    char *p = input;
    int max_length = 0;
    int current_base_length = 0;
    int current_indent_level = 0;
    int last_length = 0;
    
    int iter = 0;
    
    while(*p != '\0')
    {
        
        int indent_level = 0;
        for(; *p == '\t'; p++, indent_level++);
       
        if(indent_level == current_indent_level)
        {   
        }
        else if (indent_level < current_indent_level)
        {
            while(indent_level < current_indent_level)
            {
                current_indent_level--;
                current_base_length = stack[--stack_idx]; 
            }
        }
        else
        {
            stack[stack_idx++] = current_base_length;
            current_base_length = last_length;
            current_indent_level++;
            
        }

        bool is_file = false;
        int filename_len = 0;
        for(; *p != '\n' && *p != '\0'; p++, filename_len++)
        {
            if (*p == '.') is_file = true;
        }
        
        if(*p == '\n') p++;
        
        last_length = filename_len + current_base_length + 1;
        if(is_file && last_length > max_length)
        {
            max_length = last_length;
        }
        
    }
    
    return max_length == 0 ? 0 : (max_length - 1);
}


Post a Comment

0 Comments