FunctionRacerProblemsRace

Loop in the Wiring

EasyGraphsPython, JavaScript, TypeScript, Java, C++

A circuit has junctions numbered 0 to len(neighbors) - 1. neighbors[i] lists the junctions that share a wire with junction i. Wires are undirected and the lists are symmetric: if j is in neighbors[i] then i is in neighbors[j]. No junction is wired to itself and no wire is listed twice.

Return true if the wiring contains a loop, meaning a route that leaves a junction along some wire and comes back to it without crossing any wire twice. Otherwise return false. A circuit with no wires has no loop.

Example 1

neighbors [[1,2],[0,2],[0,1]]
returns true

Junctions 0, 1 and 2 are wired in a triangle.

Example 2

neighbors [[1],[0,2],[1]]
returns false

The three junctions form a single line, which has no loop.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems