Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Longest Absolute File Path problem solution

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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 != '')
    {
        
        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 != ''; 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);
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes