> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Mini Parser Python Solution with Tests

> Tested Python solution for LeetCode 385 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 385, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Stack](/catalog/topics/stack), [Depth-First Search](/catalog/topics/depth-first-search). [View on LeetCode](https://leetcode.com/problems/mini-parser/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 385   # by problem number
lcpy gen -s mini_parser   # by problem name
```

## Problem

Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return the deserialized `NestedInteger`.

Each element is either an integer or a list whose elements may also be integers or other lists.

### Examples

```
Input: s = "324"
Output: 324
Explanation: You should return a NestedInteger object which contains a single integer 324.
```

```
Input: s = "[123,[456,[789]]]"
Output: [123,[456,[789]]]
Explanation: Return a NestedInteger object containing a nested list with 2 elements:
1. An integer containing value 123.
2. A nested list containing two elements:
    i.  An integer containing value 456.
    ii. A nested list with one element:
         a. An integer containing value 789
```

### Constraints

* 1 \<= s.length \<= 5 \* 10\<sup>4\</sup>
* s consists of digits, square brackets `"[]"`, negative sign `'-'`, and commas `','`.
* s is the serialization of valid `NestedInteger`.
* All the values in the input are in the range \[-10\<sup>6\</sup>, 10\<sup>6\</sup>].

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/mini_parser/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/mini_parser/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from __future__ import annotations


class NestedInteger:
    def __init__(self, value: int | None = None) -> None:
        self._integer: int | None = value
        self._list: list[NestedInteger] | None = None if value is not None else []

    def is_integer(self) -> bool:
        return self._list is None

    def add(self, elem: NestedInteger) -> None:
        items = self._list
        if items is None:
            items = []
            self._integer = None
            self._list = items
        items.append(elem)

    def set_integer(self, value: int) -> None:
        self._integer = value
        self._list = None

    def get_integer(self) -> int | None:
        return self._integer

    def get_list(self) -> list[NestedInteger] | None:
        return self._list


class Solution:
    # Time: O(n) - each character is consumed exactly once
    # Space: O(d) - stack holds one NestedInteger per open bracket (d = nesting depth)
    def deserialize(self, s: str) -> NestedInteger:
        stack: list[NestedInteger] = []
        result: NestedInteger | None = None
        num: int | None = None
        i = 0
        while i < len(s):
            char = s[i]
            if char.isdigit() or char == "-":
                end = i + 1
                while end < len(s) and s[end].isdigit():
                    end += 1
                num = int(s[i:end])
                i = end
                continue
            if char == "[":
                stack.append(NestedInteger())
            else:
                if num is not None:
                    stack[-1].add(NestedInteger(num))
                    num = None
                if char == "]":
                    top = stack.pop()
                    if stack:
                        stack[-1].add(top)
                    else:
                        result = top
            i += 1
        return result if result is not None else NestedInteger(num)
```

## Complexity

| Time | Space |
| - | - |
| O(n) - each character is consumed exactly once | O(d) - stack holds one NestedInteger per open bracket (d = nesting depth) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.