Skip to content

Instantly share code, notes, and snippets.

@jovianlin
Created December 5, 2018 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jovianlin/685005da6c747b2c91d9395be04719f2 to your computer and use it in GitHub Desktop.
Save jovianlin/685005da6c747b2c91d9395be04719f2 to your computer and use it in GitHub Desktop.
Longest Absolute File Path
class Solution:
def lengthLongestPath(self, input):
"""
:type input: str
:rtype: int
"""
def is_dir(val):
return False if '.' in val else True
memo = {}
to_return = 0
arr = input.split('\n')
for val in arr:
n_level = val.count('\t')
# IS A DIRECTORY
if is_dir(val):
if n_level == 0:
memo[n_level] = len(val)
else:
memo[n_level] = len(val.replace('\t', '')) + memo[n_level-1]
# IS A FILE
else:
if n_level-1 in memo:
length = len(val.replace('\t', '')) + memo[n_level-1]
else:
length = len(val.replace('\t', ''))
length += n_level # to account for the "/" that seperates dir from another dir or file.
if length > to_return:
to_return = length
return to_return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment