Suppose we abstract our file system by a string in the following manner:

The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:

dir
    subdir1
    subdir2
        file.ext


The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.

The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents:

dir
    subdir1
        file1.ext
        subsubdir1
    subdir2
        subsubdir2
            file2.ext


The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext.

We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not including the double quotes).

Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0.

Note:

The name of a file contains at least a . and an extension.
The name of a directory or sub-directory will not contain a ..



Time complexity required: O(n) where n is the size of the input string.

Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png.

Difficulty: MEDIUM_HARD
Starter code: class Solution:
    def lengthLongestPath(self, input: str) -> int:
        

Solutions:
class Solution:

	def lengthLongestPath(self, input):
		dict = {0: 0}
		maxlen = 0
		line = input.split('\n')
		for i in line:
			name = i.lstrip('\t')
			print(name)
			print(len(name))
			depth = len(i) - len(name)
			if '.' in name:
				maxlen = max(maxlen, dict[depth] + len(name))
			else:
				dict[depth + 1] = dict[depth] + len(name) + 1
		return maxlen


class Solution:

	def lengthLongestPath(self, input):
		maxlen = 0
		path = {0: 0}
		for line in input.splitlines():
			name = line.lstrip('\t')
			depth = len(line) - len(name)
			if '.' in line:
				maxlen = max(maxlen, path[depth] + len(name))
			else:
				path[depth + 1] = path[depth] + len(name) + 1
		return maxlen


class Solution:

	def lengthLongestPath(self, inp):
		(m, l) = (0, {-1: -1})
		for s in inp.split('\n'):
			d = s.count('\t')
			l[d] = 1 + l[d - 1] + len(s) - d
			if '.' in s:
				m = max(m, l[d])
		return m


class Solution:

	def lengthLongestPath(self, input):
		lines = input.split('\n')
		paths = []
		ans = 0
		for line in lines:
			name = line.strip('\t')
			depth = len(line) - len(name)
			if '.' not in name:
				if depth >= len(paths):
					paths.append(len(name) + 1)
				else:
					paths[depth] = len(name) + 1
			else:
				ans = max(ans, sum(paths[:depth]) + len(name))
		return ans


class Solution:

	def lengthLongestPath(self, input):
		max_line = 0
		path_size = {0: 0}
		lines = input.splitlines()
		for line in lines:
			stripped = line.lstrip('\t')
			depth = len(line) - len(stripped)
			if '.' in stripped:
				max_line = max(max_line, path_size[depth] + len(stripped))
			else:
				path_size[depth + 1] = path_size[depth] + len(stripped) + 1
		return max_line


class Solution:

	def lengthLongestPath(self, input):
		(res, l) = (0, 0)
		path = []
		for line in input.splitlines():
			name = line.lstrip('\t')
			n = len(line) - len(name)
			while len(path) > n:
				l -= path[-1]
				path.pop()
			l += len(name)
			path.append(len(name))
			if '.' in name:
				res = max(res, l + len(path) - 1)
		return res


class Solution:

	def lengthLongestPath(self, input):
		dir = []
		tab = 0
		curr = []
		temp = ''
		op = 0
		input += '\n'
		for c in input:
			print('start:', c, temp)
			if c == '\n' and temp != '':
				if curr:
					print(curr)
					while tab <= curr[-1]:
						dir.pop(-1)
						curr.pop(-1)
						if not curr:
							break
				dir.append(temp)
				curr.append(tab)
				print(dir, curr)
				if '.' in temp:
					ans = '/'.join(dir)
					op = max(len(ans), op)
				temp = ''
				tab = 0
			elif c == '\t':
				tab += 1
			else:
				temp += c
		return op


class Solution:

	def lengthLongestPath(self, x):
		h = {0: 0}
		maxlen = 0
		for line in x.splitlines():
			name = line.lstrip('\t')
			depth = len(line) - len(name)
			if name.count('.') != 0:
				maxlen = max(maxlen, h[depth] + len(name))
			else:
				h[depth + 1] = h[depth] + len(name) + 1
		return maxlen


class Solution:

	def lengthLongestPath(self, input):
		max_len = 0
		depth2len = {0: 0}
		for line in input.splitlines():
			name = line.lstrip('\t')
			depth = len(line) - len(name)
			if '.' in name:
				max_len = max(max_len, depth2len[depth] + len(name))
			else:
				depth2len[depth + 1] = depth2len[depth] + len(name) + 1
		return max_len