A testing tool stores DOM snapshots in a compact text form. An element is its tag name, optionally followed by its children in parentheses, separated by commas. For example div(h1,ul(li,li)) is a div holding an h1 and a ul, and the ul holds two lis. Tag names use lowercase letters and digits; there are no spaces.
Write parseSnapshot(text) that returns the root element as nested objects { "tag": ..., "children": [...] }. Elements without parentheses have children: []. Parentheses are never empty, and the text is always valid.
Input: text = "div(h1,ul(li,li))" Output: {"tag":"div","children":[{"tag":"h1","children":[]},{"tag":"ul","children":[{"tag":"li","children":[]},{"tag":"li","children":[]}]}]}
h1 has no parentheses, so it has no children; ul has two.
Input: text = "p" Output: {"tag":"p","children":[]}
A single element with no children.
1 <= text.length <= 10^4Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #365 Parse a Compact DOM Snapshot. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.