rooms rooms numbered from 1 to rooms. Some pairs of rooms are linked by doors: doors[i] = [a, b] means you can walk between rooms a and b in either direction.
A three-room loop is a set of three distinct rooms where each room has a door to both of the others. Two loops are the same if they consist of the same rooms, no matter the order you walk them in.
Return the number of distinct three-room loops.rooms=5, doors=[[1, 2], [2, 3], [3, 1], [3, 4], [4, 5]] → 1
The only loop is made of rooms 1, 2 and 3.
rooms=4, doors=[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] → 4
Every pair of the four rooms is connected, so any three rooms form a loop: {1,2,3}, {1,2,4}, {1,3,4}, {2,3,4}.
rooms=3, doors=[[1, 2], [2, 3]] → 0
There is no door from room 3 back to room 1, so there is no loop.
rooms=5, doors=[[1, 2], [2, 3], [3, 1], [3, 4], [4, 5]]
1
Run checks the first 3 tests in your browser. Submit checks all 60 tests on the server, hidden ones included.