← 返回 bytedance 的题目列表Parse Nested ArrayList-like String to Nested List
类型:online_judge
Problem: Parse a Nested ArrayList-like String
You are given a string s representing an ArrayList<String>. The list may be nested. Parse it into an in-memory nested list structure (e.g., a Python list whose elements are either strings or sub-lists).
Input Format (guaranteed valid)
Lists are denoted by square brackets: [ and ].
Elements are separated by commas ,.
Each element is either:
a double-quoted string, e.g. "abc", or
another list (nested).
String contents contain only letters and digits (no quotes/commas/brackets/backslashes), so no escaping needs to be handled.
Whitespace (spaces/newlines/tabs) may appear and should be ignored.
Output
Print the parsed nested list in a canonical form for judging:
Use JSON-style printing (same as Python json.dumps):
strings use double quotes
lists use square brackets
no strict requirement on spaces
Constraints
1 <= len(s) <= 2 * 10^5
Nesting depth <= 10^4
Examples
Input:
["a","b",["c","d"],"e"]
Output:
["a","b",["c","d"],"e"]
Input:
["x",[]]
Output:
["x",[]]
Requirement
Solve in O(n) time; using a stack is recommended.
Example
Input
["a","b",["c","d"],"e"]
Output
["a","b",["c","d"],"e"]