Friends in Common
Each entry of friendships is a pair [x, y]: persons x and y are friends. Friendship goes both ways, and a pair may be listed more than once.
Return the persons who are friends with both a and b, in increasing order and each only once.
Example 1
friendships [[0,2],[2,1],[0,3]]
a 0
b 1
returns [2]
Person 2 is a friend of both 0 and 1. Person 3 is a friend of 0 only.
Example 2
friendships [[0,1]]
a 0
b 1
returns []
Persons 0 and 1 are friends with each other, but they share no third friend.
Example 3
friendships []
a 0
b 1
returns []
Nobody has any friends.
Constraints
- persons are numbered
0to11 0 <= len(friendships) <= 30, and nobody is listed as their own frienda != b
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Graphs problems
- Is There a RoadBeginner
- Next DoorBeginner
- No HandshakeBeginner
- Who Points at MeBeginner
- Busiest NodeEasy
- Cable GroupsEasy
- Count the LakesEasy
- Everything Needed FirstEasy