|
| 1 | +--- |
| 2 | +id: mini-parser |
| 3 | +title: Mini Parser |
| 4 | +sidebar_label: 0385 - Mini Parser |
| 5 | +tags: |
| 6 | + - String |
| 7 | + - Stack |
| 8 | + - Depth First Search |
| 9 | +description: "This is a solution to the Mini Parser problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return the deserialized `NestedInteger`. |
| 15 | + |
| 16 | +Each element is either an integer or a list whose elements may also be integers or other lists. |
| 17 | + |
| 18 | +### Examples |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +``` |
| 23 | +Input: s = "324" |
| 24 | +Output: 324 |
| 25 | +Explanation: You should return a NestedInteger object which contains a single integer 324. |
| 26 | +
|
| 27 | +``` |
| 28 | +**Example 2:** |
| 29 | +``` |
| 30 | +
|
| 31 | +Input: s = "[123,[456,[789]]]" |
| 32 | +Output: [123,[456,[789]]] |
| 33 | +Explanation: Return a NestedInteger object containing a nested list with 2 elements: |
| 34 | +1. An integer containing value 123. |
| 35 | +2. A nested list containing two elements: |
| 36 | + i. An integer containing value 456. |
| 37 | + ii. A nested list with one element: |
| 38 | + a. An integer containing value 789 |
| 39 | +
|
| 40 | +``` |
| 41 | +### Constraints |
| 42 | + |
| 43 | +- `1 <= s.length <= 5 * 10^4` |
| 44 | +- `s is the serialization of valid NestedInteger` |
| 45 | +- `All the values in the input are in the range [-10^6, 10^6]` |
| 46 | + |
| 47 | +## Solution for Mini Parser |
| 48 | + |
| 49 | +### Approach |
| 50 | + |
| 51 | +This solution uses a stack to record the `NestedInteger`'s. |
| 52 | +At the very beginning, an empty NestedInteger is placed in the stack. This NestedInteger will be regarded as a list that holds one but only one NestedInteger, which will be returned in the end. |
| 53 | +`Logic: When encountering '[', the stack has one more element. When encountering ']', the stack has one less element`. |
| 54 | + |
| 55 | +## Code in Different Languages |
| 56 | + |
| 57 | +<Tabs> |
| 58 | +<TabItem value="cpp" label="C++"> |
| 59 | + <SolutionAuthor name="@agarwalhimanshugaya"/> |
| 60 | + |
| 61 | +```cpp |
| 62 | +class Solution { |
| 63 | +public: |
| 64 | + NestedInteger deserialize(string s) { |
| 65 | + function<bool(char)> isnumber = [](char c){ return (c == '-') || isdigit(c); }; |
| 66 | + |
| 67 | + stack<NestedInteger> stk; |
| 68 | + stk.push(NestedInteger()); |
| 69 | + |
| 70 | + for (auto it = s.begin(); it != s.end();) { |
| 71 | + const char & c = (*it); |
| 72 | + if (isnumber(c)) { |
| 73 | + auto it2 = find_if_not(it, s.end(), isnumber); |
| 74 | + int val = stoi(string(it, it2)); |
| 75 | + stk.top().add(NestedInteger(val)); |
| 76 | + it = it2; |
| 77 | + } |
| 78 | + else { |
| 79 | + if (c == '[') { |
| 80 | + stk.push(NestedInteger()); |
| 81 | + } |
| 82 | + else if (c == ']') { |
| 83 | + NestedInteger ni = stk.top(); |
| 84 | + stk.pop(); |
| 85 | + stk.top().add(ni); |
| 86 | + } |
| 87 | + ++it; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + NestedInteger result = stk.top().getList().front(); |
| 92 | + return result; |
| 93 | + } |
| 94 | +}; |
| 95 | +``` |
| 96 | +</TabItem> |
| 97 | +<TabItem value="java" label="Java"> |
| 98 | + <SolutionAuthor name="@agarwalhimanshugaya"/> |
| 99 | +
|
| 100 | +```java |
| 101 | +public NestedInteger deserialize(String s) { |
| 102 | + if (s.isEmpty()) |
| 103 | + return null; |
| 104 | + if (s.charAt(0) != '[') // ERROR: special case |
| 105 | + return new NestedInteger(Integer.valueOf(s)); |
| 106 | + |
| 107 | + Stack<NestedInteger> stack = new Stack<>(); |
| 108 | + NestedInteger curr = null; |
| 109 | + int l = 0; // l shall point to the start of a number substring; |
| 110 | + // r shall point to the end+1 of a number substring |
| 111 | + for (int r = 0; r < s.length(); r++) { |
| 112 | + char ch = s.charAt(r); |
| 113 | + if (ch == '[') { |
| 114 | + if (curr != null) { |
| 115 | + stack.push(curr); |
| 116 | + } |
| 117 | + curr = new NestedInteger(); |
| 118 | + l = r+1; |
| 119 | + } else if (ch == ']') { |
| 120 | + String num = s.substring(l, r); |
| 121 | + if (!num.isEmpty()) |
| 122 | + curr.add(new NestedInteger(Integer.valueOf(num))); |
| 123 | + if (!stack.isEmpty()) { |
| 124 | + NestedInteger pop = stack.pop(); |
| 125 | + pop.add(curr); |
| 126 | + curr = pop; |
| 127 | + } |
| 128 | + l = r+1; |
| 129 | + } else if (ch == ',') { |
| 130 | + if (s.charAt(r-1) != ']') { |
| 131 | + String num = s.substring(l, r); |
| 132 | + curr.add(new NestedInteger(Integer.valueOf(num))); |
| 133 | + } |
| 134 | + l = r+1; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return curr; |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +</TabItem> |
| 143 | +<TabItem value="python" label="Python"> |
| 144 | + <SolutionAuthor name="@agarwalhimanshugaya"/> |
| 145 | + |
| 146 | +```python |
| 147 | +class Solution: |
| 148 | + def deserialize(self, s): |
| 149 | + stack, num, last = [], "", None |
| 150 | + for c in s: |
| 151 | + if c.isdigit() or c == "-": num += c |
| 152 | + elif c == "," and num: |
| 153 | + stack[-1].add(NestedInteger(int(num))) |
| 154 | + num = "" |
| 155 | + elif c == "[": |
| 156 | + elem = NestedInteger() |
| 157 | + if stack: stack[-1].add(elem) |
| 158 | + stack.append(elem) |
| 159 | + elif c == "]": |
| 160 | + if num: |
| 161 | + stack[-1].add(NestedInteger(int(num))) |
| 162 | + num = "" |
| 163 | + last = stack.pop() |
| 164 | + return last if last else NestedInteger(int(num)) |
| 165 | +``` |
| 166 | +</TabItem> |
| 167 | +</Tabs> |
| 168 | + |
| 169 | +## Complexity Analysis |
| 170 | + |
| 171 | +### Time Complexity: $O(N)$ |
| 172 | + |
| 173 | +### Space Complexity: $O(N)$ |
| 174 | + |
| 175 | +## References |
| 176 | + |
| 177 | +- **LeetCode Problem**: [Kth Largest Element in a Stream](https://leetcode.com/problems/mini-parser/description/) |
| 178 | + |
| 179 | +- **Solution Link**: [Kth Largest Element in a Stream](https://leetcode.com/problems/mini-parser/solutions/) |
0 commit comments