## Algorithm

Problem Name: 385. Mini Parser

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.

Example 1:

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

Example 2:

```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 * 104`
• `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 `[-106, 106]`.

## Code Examples

### #1 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public NestedInteger deserialize(String s) {
if (s.isEmpty()) {
return null;
}
if (s.charAt(0) != '[') {
return new NestedInteger(Integer.parseInt(s));
}
Stack < NestedInteger> stack = new Stack<>();
NestedInteger curr = null;
int leftIdx = 0;
for (int rightIdx = 0; rightIdx  <  s.length(); rightIdx++) {
if (s.charAt(rightIdx) == '[') {
if (curr != null) {
stack.push(curr);
}
curr = new NestedInteger();
leftIdx = rightIdx + 1;
} else if (s.charAt(rightIdx) == ']') {
String number = s.substring(leftIdx, rightIdx);
if (!number.isEmpty()) {
}
if (!stack.isEmpty()) {
NestedInteger popped = stack.pop();
curr = popped;
}
leftIdx = rightIdx + 1;
} else if (s.charAt(rightIdx) == ',') {
if (s.charAt(rightIdx - 1) != ']') {
String number = s.substring(leftIdx, rightIdx);
}
leftIdx = rightIdx + 1;
}
}
return curr;
}
}
``````
Copy The Code &

Input

cmd
s = "324"

Output

cmd
324

### #2 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const deserialize = function(s) {
const recursion = s => {
const re = new NestedInteger()
if (!s || s.length === 0) {
return re
}
if (s.charAt(0) !== '[') {
re.setInteger(parseInt(s, 10))
} else if (s.length > 2) {
let start = 1
let cnt = 0
for (let i = 1; i  <  s.length; i++) {
const char = s.charAt(i)
if (cnt === 0 && (char === ',' || i === s.length - 1)) {
start = i + 1
} else if (char === '[') {
cnt++
} else if (char === ']') {
cnt--
}
}
}
return re
}
return recursion(s)
}
``````
Copy The Code &

Input

cmd
s = "324"

Output

cmd
324

### #3 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def deserialize(self, s):
stack, num, last = [], "", None
for c in s:
if c.isdigit() or c == "-": num += c
elif c == "," and num:
num = ""
elif c == "[":
elem = NestedInteger()
stack.append(elem)
elif c == "]":
if num:
num = ""
last = stack.pop()
return last if last else NestedInteger(int(num))
``````
Copy The Code &

Input

cmd
s = "[123,[456,[789]]]"

Output

cmd
[123,[456,[789]]]