Minimum Friend API Calls
Problem statement
A friendship service exposes one operation: querying a user returns that user's direct friends. Each query counts as one API call.
You are given the complete undirected friendship pairs for an offline practice adapter, plus source and target. Return the minimum number of friend-list API calls along any chain from source to target. Return 0 when they are the same user and -1 when no chain exists.
Function
minimumFriendApiCalls(friendships: String[][], source: String, target: String) → intExamples
Example 1
friendships = [["a","b"],["b","c"],["c","d"]]source = "a"target = "d"return = 3The shortest chain has three friendship edges.
Example 2
friendships = [["alice","bob"]]source = "alice"target = "bob"return = 1One query reaches the direct friend.
Example 3
friendships = [["a","b"],["c","d"]]source = "a"target = "d"return = -1The users are in different connected components.
Constraints
0 <= friendships.length <= 100000- Every pair contains two distinct nonempty user IDs of at most 40 characters.
sourceandtargetare nonempty user IDs.- Duplicate friendship pairs do not change the result.