class Solution:
	def lengthLongestPath(self, input: str) -> int:
		if "." not in input:
			return 0

		a=input.split("\n")
		files=[]
		for i in a:
			if "." in i:
				files.append(i)

		final=[]
		for i in range(len(files)):
			file=files[i]
			lvl=file.count("\t")
			idx=a.index(file)-1
			save=[files[i].replace("\t","")]
			for j in range(lvl):
				while a[idx].count("\t")!=lvl-1:
					idx-=1
				lvl=a[idx].count("\t")
				save.append(a[idx].replace("\t",""))
				idx-=1
			final.append(save)

		final=list(map("/".join,final))
		return len(max(final,key=len))
class Solution {
    public int lengthLongestPath(String input) {
        var stack = new ArrayDeque<Integer>();
        int max = 0;
        String[] lines = input.split("\n");
        for(var line: lines) {
            int tabs = countTabs(line);
            while(tabs < stack.size()) {
                stack.pop();
            }
            int current = stack.isEmpty() ? 0: stack.peek();
            int nameLength = line.length() - tabs;
            if(isFilename(line)) {
                max = Math.max(max, current + nameLength);
            } else {
                stack.push(current + nameLength + 1);
            }
        }
        return max;
    }
    
    private int countTabs(String s) {
        for(int i = 0; i < s.length(); i++) {
            if(s.charAt(i) != '\t') return i;
        }
        return 0;
    }
    
    private boolean isFilename(String s) {
        return s.lastIndexOf(".") != -1;
    }
    
}
Longest Absolute File Path
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:



Here, we have dir as the only directory in the root. dir contains two subdirectories, subdir1 and subdir2. subdir1 contains a file file1.ext and subdirectory subsubdir1. subdir2 contains a subdirectory subsubdir2, which contains a file file2.ext.

In text form, it looks like this (with ⟶ representing the tab character):

dir
⟶ subdir1
⟶ ⟶ file1.ext
⟶ ⟶ subsubdir1
⟶ subdir2
⟶ ⟶ subsubdir2
⟶ ⟶ ⟶ file2.ext


If we were to write this representation in code, it will look like this: "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext". Note that the '\n' and '\t' are the new-line and tab characters.

Every file and directory has a unique absolute path in the file system, which is the order of directories that must be opened to reach the file/directory itself, all concatenated by '/'s. Using the above example, the absolute path to file2.ext is "dir/subdir2/subsubdir2/file2.ext". Each directory name consists of letters, digits, and/or spaces. Each file name is of the form name.extension, where name and extension consist of letters, digits, and/or spaces.

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.

Note that the testcases are generated such that the file system is valid and no file or directory name has length 0.

&nbsp;
Example 1:

Input: input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
Output: 20
Explanation: We have only one file, and the absolute path is "dir/subdir2/file.ext" of length 20.


Example 2:

Input: input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
Output: 32
Explanation: We have two files:
"dir/subdir1/file1.ext" of length 21
"dir/subdir2/subsubdir2/file2.ext" of length 32.
We return 32 since it is the longest absolute path to a file.


Example 3:

Input: input = "a"
Output: 0
Explanation: We do not have any files, just a single directory named "a".


&nbsp;
Constraints:


	1 &lt;= input.length &lt;= 104
	input may contain lowercase or uppercase English letters, a new line character '\n', a tab character '\t', a dot '.', a space ' ', and digits.
	All file and directory names have positive length.


function isFile(path) {
    return path.includes('.')
}

var lengthLongestPath = function(input) {
    const segments = input.split('\n');
    
    let max = 0;
    let path = [];
    for (const segment of segments) {
        if (segment.startsWith('\t')) {
            const nesting = segment.match(/\t/g).length;
            
            while (nesting < path.length) {
                path.pop();
            }
            
            path.push(segment.replace(/\t/g, ''))
        } else {
            path = [segment]
        }
        
        if (isFile(path.at(-1))) {
            const filePath = path.join('/');
            if (filePath.length > max) {
                max = filePath.length;
            }
        }
    }
    
    return max;
};
// Using Map O(300 + N)

class Solution {
    public:
    int lengthLongestPath(string input) {
        input.push_back('\n');
        vector<int> levels(301, 0);
        int ans = 0;
        int curr_tabs = 0;
        bool is_file = false;
        int curr_word_len = 0;
        int total_len = 0;

        for(char c : input)
        {
            if(c == '\t')
            {
                curr_tabs++;
            }
            else if(c == '\n')
            {
                if(curr_tabs == 0)
                {
                    levels[0] = curr_word_len;
                }
                else
                {
                    levels[curr_tabs] = levels[curr_tabs-1] + curr_word_len;
                }

                if(is_file)
                {
                    ans = max(ans, levels[curr_tabs] + curr_tabs);
                    // levels[curr_tabs] = 0;
                }
                curr_tabs = 0;
                is_file = false;
                curr_word_len = 0;
            }
            else if(c == '.')
            {
                is_file = true;
                curr_word_len++;
            }
            else
            {
                curr_word_len++;
            }
        }
        return ans;
    }
};