O(n) solution using stack in Python


  • 0
    R
    class Solution(object):
        def lengthLongestPath(self, input):
            """
            :type input: str
            :rtype: int
            """
            st = []
            longest = 0
            for t in input.split('\n'):
                while st and st[-1].rfind('\t') >= t.rfind('\t'):
                    st.pop()
                st += t,
                if '.' in t:
                    newstr = '/'.join([s.lstrip('\t') for s in st])
                    longest = max(len(newstr), longest)
            return longest
    

Log in to reply
 

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