Stack solution


  • 0
    J

    Just thought about using stack when I saw the structure of multiple '\t'.

    import string
    
    
    class Solution(object):
        def lengthLongestPath(self, input):
            """
            :type input: str
            :rtype: int
            """
            cur_dirs = []
            longest_path = 0
    
            for entry in input.split('\n'):
                depth = entry.count('\t')
                entry = entry[depth:]
                if '.' in entry:
                    longest_path = max(longest_path, len(string.join(cur_dirs[:depth] + [entry], '/')))
                else:
                    cur_dirs = cur_dirs[:depth] + [entry]
    
            return longest_path
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.