Convert String to Map

Difficulty: Medium · Topics: String Parsing, Map, Conversion · Acceptance: 50.0% of 2 submissions

Difficulty: Medium  |  Tags: String Parsing, Map, Conversion

Description

Given a string representation of key-value pairs, parse it into an actual Map.

The string follows formats like:

Handle nested maps and list values where possible.

Implement:

stringToMap(str) → Map<String, Object>

Examples

Example 1:

Input:  "{name=Alice, age=30, city=NYC}"
Output: {"name": "Alice", "age": "30", "city": "NYC"}

Example 2:

Input:  "host=localhost, port=8080, debug=true"
Output: {"host": "localhost", "port": "8080", "debug": "true"}

Example 3 — Nested:

Input:  "{user={name=Alice, role=admin}, active=true}"
Output: {"user": {"name": "Alice", "role": "admin"}, "active": "true"}

Constraints

Hints

Hint 1

Split on ,, then split each token on =. Be careful with nested braces — don't split inside them.

Practice this problem

Related problems