Mini Parser LeetCode solution in Python
1class Solution:2"""3we can approach this problem using stack4"""56def dfs(self, i, s):7res = NestedInteger()8while i < len(s):9if s[i] == "[":10y, i = self.dfs(i + 1, s)11res.add(y)12elif i < len(s) and s[i] == "]":13i += 114return res, i15elif i < len(s) and s[i] == ",":16i += 117else:18if i < len(s):19start = i20while s[i] != "," and s[i] != "]":21i += 122res.add(NestedInteger(int(s[start:i])))23return res, i2425def deserialize(self, s: str) -> NestedInteger:26if s[0] == "[":27res, i = self.dfs(1, s)28return res29else:30num = int(s)31res = NestedInteger()32res.setInteger(num)33return res