Medium · Hash Maps

Sort pairs

Given a map of origin → destination pairs that together form a single chain (no branches or cycles), return the pairs as "origin-destination" strings in the order the chain is traveled, starting from the origin that is never a destination.

Examples

Example 1

{
  "pairs": [
    ["c", "d"],
    ["a", "b"],
    ["b", "c"]
  ]
}

Output: a-b, b-c, c-d

Example 2

{
  "pairs": [
    ["y", "z"],
    ["x", "y"]
  ]
}

Output: x-y, y-z

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Hash Maps problems