Mini Parser LeetCode Solution

Here, We see Mini Parser LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Mini Parser LeetCode Solution

Mini Parser LeetCode Solution

Problem Statement

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

Mini Parser LeetCode Solution C++

class Solution {
    NestedInteger deserialize(string s) {
        istringstream in(s);
        return deserialize(in);
    NestedInteger deserialize(istringstream &in) {
        int number;
        if (in >> number)
            return NestedInteger(number);
        NestedInteger list;
        while (in.peek() != ']') {
            if (in.peek() == ',')
        return list;
};Code language: PHP (php)

Mini Parser LeetCode Solution Java

class Solution {
    public NestedInteger deserialize(String s) {
        if(s.length()==0 || s.equals("[]")) return new NestedInteger();
        NestedInteger ni = new NestedInteger();
        char[] chars = s.toCharArray();
        if(chars[0]=='[') {
            int elementStart = 1;
            while(elementStart<chars.length) {
                int elementEnd = searchForElementEnd(chars,elementStart);
                String nextListElement = new String(chars,elementStart,elementEnd-elementStart);
        } else ni.setInteger(new Integer(new String(chars)));
        return ni;

    private int searchForElementEnd(char[] chars, int elementStart) {
        int countBrackets = 0;
        int i=elementStart;
        if(chars[i++]=='[') countBrackets++;
        while(i<chars.length) {
            char nextChar = chars[i];
            if(nextChar==']') {
                if(countBrackets<=0) {
                    if(countBrackets==0) i++;
            else if(nextChar=='[') countBrackets++;
            else if(nextChar==',' && countBrackets==0) {
        return i;
}Code language: JavaScript (javascript)

Mini Parser Solution JavaScript

var deserialize = function(s) {
    if(!s) return null
    s = JSON.parse(s)
    let n = new NestedInteger()
    function fn(nt, s) {
        if(Array.isArray(s)) {
            let a = new NestedInteger()
            for(let item of s) {
                let ret = fn(a, item)
            return a
        return new NestedInteger(s)
    let ret = fn(n, s)
    return ret
};Code language: JavaScript (javascript)

Mini Parser Solution Python

class Solution(object):
    def deserialize(self, s):
        def nestedInteger(x):
            if isinstance(x, int):
                return NestedInteger(x)
            lst = NestedInteger()
            for y in x:
            return lst
        return nestedInteger(eval(s))
Scroll to Top