1/19/2021

[LeetCode] 385. Mini Parser

 Problem : https://leetcode.com/problems/mini-parser/

Time Complexity = O (N ** 2)


class Solution:
    def deserialize(self, s: str) -> NestedInteger:
        if not s: return NestedInteger()
           
        if s[0] != '[': return NestedInteger(int(s))
        
        if len(s) <= 2: return NestedInteger()
        
        result = NestedInteger()
        
        left = 1
        lb = rb = 0
        
        for right in range(1, len(s)):
            if s[right] == '[':
                lb += 1
            elif s[right] == ']':
                rb += 1
                
            if (lb == rb and s[right] == ',') or right == len(s) - 1:
                result.add(self.deserialize(s[left:right]))
                left = right + 1
                
        return result

No comments:

Post a Comment