Python with Stack

  • 0

    Not as cool as this one but thought I'd post anyways.

    class Solution(object):
        def lengthLongestPath(self, input):
            stack,mx = collections.deque(),0
            for line in input.splitlines():
                tabCount = line.count('\t')
                for _ in xrange(len(stack)-tabCount):
                length = (stack[-1]+1 if len(stack) > 0 else 0) + len(line) - tabCount
                if '.' in line:
                    mx = max(mx,length)
            return mx

Log in to reply

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