Created
December 5, 2018 03:43
-
-
Save jovianlin/685005da6c747b2c91d9395be04719f2 to your computer and use it in GitHub Desktop.
Longest Absolute File Path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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