Mutual Follows
follows[i] is the list of accounts that account i follows. Accounts are numbered 0 to len(follows) - 1. Two different accounts a and b are mutual when a follows b and b follows a.
Return the number of mutual pairs. Each pair of accounts is counted once, no matter how many times the accounts appear in each other's lists. An account may follow itself; that never makes a mutual pair.
Example 1
follows [[1,2],[0],[0,1]]
returns 2
0 and 1 follow each other, and so do 0 and 2. Account 2 follows 1 but 1 does not follow 2 back.
Example 2
follows [[1],[2],[0]]
returns 0
The follows go around in a circle, so nobody is followed back.
Constraints
1 <= len(follows) <= 500 <= follows[i][j] < len(follows)- At most 100 entries in total
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Graphs problems
- Paint BucketEasy
- Pointer Loop LengthEasy
- Rumour DaysEasy
- Same Network QueriesEasy
- Tunnels to Every StationEasy
- Blocks to the Nearest ShopMedium
- Build Order ExistsMedium
- Calm SquaresMedium