Problem
FAANG · LeetCode #385

Mini Parser — three-stage drill

1The ProblemMedium

Mini Parser

Deserialize a string like "[123,[456,789]]" into its nested-integer structure.

deserialize the string[123,[456,789]]a list whose elements are integers or other lists[ 123, [ 456, 789 ] ]depthup to 2 levels
Examples
"324"→ 324"[123,[456,789]]"→ [123, [456, 789]]

constraintThe string is a valid nested-list encoding; integers may be negative.

2The PatternTIME O(n) · SPACE O(d)

Recursive Descent

Parse a structured string top-down: each grammar rule is a function, and one lookahead character tells you which to call next. Nesting is handled by recursion, and — unlike backtracking — a choice is never undone, because the next character is always enough to decide.

the next character alone decides what to parse — recurse on nesting, never undoinput20*1(233+445)6scan›call stack — one frame per nested groupouterexprstart → call parse_expr, read 2
Problems using this pattern
3The SolutionRESULT → [12, [34]]

Step through it

recursive descent — one lookahead char picks the branchinput[01122,3[43546]7]8i›call stackouterlistbuilt so far[ ]'[' → start a list, recurse into parse_list
'[' → start a list, recurse into parse_list
Step 1 / 7
Arrow keys to navigate · escape to reset

All problems by pattern

Backtracking

Recursive Descent