Skip to content
LC-0388 Medium LeetCode

388. Longest Absolute File Path

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 48% Topics: String, Stack, Depth-First Search
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(d), d is the max depth of the paths

class Solution(object):
    def lengthLongestPath(self, input):
        """
        :type input: str
        :rtype: int
        """
        def split_iter(s, tok):
            start = 0
            for i in xrange(len(s)):
                if s[i] == tok:
                    yield s[start:i]
                    start = i + 1
            yield s[start:]


        max_len = 0
        path_len = {0: 0}
        for line in split_iter(input, '\n'):
            name = line.lstrip('\t')
            depth = len(line) - len(name)
            if '.' in name:
                max_len = max(max_len, path_len[depth] + len(name))
            else:
                path_len[depth + 1] = path_len[depth] + len(name) + 1
        return max_len

Solution from kamyu104/LeetCode-Solutions · MIT