Convert Nested Key-Value Pairs to YAML Format

Problem Given a list of slash-delimited key paths mapped to values, convert them into a nested YAML-formatted string, grouping shared path prefixes under common parent keys with correct indentation.

Input / Output

  • Input: a list of path: value entries where each path is slash-delimited (e.g. /web/leetcode/contest: 435).
  • Output: a single YAML string in which shared prefixes collapse into one parent key and every nesting level is indented two spaces.

Constraints

  • Paths may share prefixes to arbitrary depth, and the same segment name may appear under different parents.
  • Indentation must be consistent — two spaces per level is the usual convention.
  • Values are scalars (numbers or strings); the leading slash is a separator artifact, not an empty root segment.
  • Worth clarifying up front: whether sibling order must follow input order, and whether a path can be both a leaf and a parent (/a: 1 alongside /a/b: 2) — a conflict the structure must reject or resolve.

Example Input:

/web/leetcode/contest: 435
/web/leetcode/discuss: Zomato
/web/leetcode/premium/amount: 15000
/leetcode/explore: interviewExp
/leetcode/store: premium

Output:

web:
  leetcode:
    contest: 435
    discuss: Zomato
    premium:
      amount: 15000
leetcode:
  explore: interviewExp
  store: premium

Note that leetcode appears both nested under web and as a separate top-level key — the case proving grouping is per-parent, not global by segment name.

asked …
LeaderboardSalaryAccount