You are building the connection graph for a social platform.
Given a list of friendships connections, where each element is a pair [A, B] indicating that user A and user B are friends, design a data structure that preprocesses the graph and supports efficient connection lookups.
Friendships are bidirectional — if A is friends with B, then B is friends with A.
Implement the SocialNetwork class:
__init__(self, connections: list[list[str]]) — builds the internal graph from the connection listget_connections(self, user: str) -> list[str] — returns a list of all direct friends of user. If the user has no connections or does not appear in the graph, return an empty list.get_connections must run in O(1) time
Example 1:
Input: connections = [["Alice", "Bob"], ["Alice", "Charlie"], ["Charlie", "Dirk"]], user = "Alice"
Output: ["Bob", "Charlie"]
Explanation: Alice is directly connected to Bob and Charlie.
Example 2:
Input: connections = [["Alice", "Bob"], ["Alice", "Charlie"], ["Charlie", "Dirk"]], user = "Charlie"
Output: ["Alice", "Dirk"]
Explanation: Charlie is directly connected to Alice and Dirk.
Example 3:
Input: connections = [["Alice", "Bob"], ["Bob", "David"]], user = "Alice"
Output: ["Bob"]
Explanation: Alice is only directly connected to Bob.
Constraints:
0 <= len(connections) <= 10^4len(connections[i]) = 21 <= len(connections[i][j]) <= 11